当前位置:网站首页>剑指 Offer II 105. 岛屿的最大面积
剑指 Offer II 105. 岛屿的最大面积
2022-07-01 16:28:00 【彼淇梁】
剑指 Offer II 105. 岛屿的最大面积【中等题】
思路:【DFS】
每遇到一个岛,记录岛屿数量为1,同时以这个岛为中心向上下左右四个方向搜索相邻岛屿并记录岛屿数量,同时将这个岛炸沉(置为0),表示已搜索过。
当一个岛四个方向搜索完毕,更新最大岛屿数量,由于每个岛屿面积一样,所以最大岛屿数量即最大岛屿面积。
代码:
class Solution {
static int m;
static int n;
public int maxAreaOfIsland(int[][] grid) {
m = grid.length;
n = grid[0].length;
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] != 0){
ans = Math.max(ans,dfs(i,j,grid));
}
}
}
return ans;
}
public int dfs(int x,int y,int[][] grid){
if (x < 0 || y < 0 || x == m || y == n || grid[x][y] == 0){
return 0;
}
grid[x][y] = 0;
int[][] f = {
{
0,1},{
0,-1},{
-1,0},{
1,0}};
int cnt = 1;
for (int i = 0; i < 4; i++) {
int dx = x + f[i][0],dy = y + f[i][1];
cnt += dfs(dx,dy,grid);
}
return cnt;
}
}
边栏推荐
- AI高考志愿填报:大厂神仙打架,考生付费围观
- Buuctf gold III
- Basic usage of Frida
- 数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
- AI college entrance examination volunteer filling: the gods of Dachang fight, and candidates pay to watch
- Tutorial on the principle and application of database system (002) -- MySQL installation and configuration: MySQL software uninstallation (Windows Environment)
- How to cancel automatic search and install device drivers for laptops
- 想做软件测试的女孩子看这里
- C语言输入/输出流和文件操作
- [pyg] document summary and project experience (continuously updated
猜你喜欢
String class
Is the programmer's career really short?
Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
Bugku's file contains
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
Redis 分布式锁
想做软件测试的女孩子看这里
数据库系统原理与应用教程(002)—— MySQL 安装与配置:MySQL 软件的卸载(windows 环境)
数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
阿里云、追一科技抢滩对话式AI
随机推荐
Go language source level debugger delve
模板引擎Velocity 基礎
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
How to solve the problem that the battery icon of notebook computer does not display
Research and investment strategy report of neutral protease industry in China (2022 Edition)
Endeavouros mobile hard disk installation
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
Hi Fun Summer, play SQL planner with starrocks!
Advantages, values and risks of chain games compared with traditional games
Why is the pkg/errors tripartite library more recommended for go language error handling?
How to repair the laptop that cannot connect to the wireless network
[nodemon] app crashed - waiting for file changes before starting... resolvent
数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)
sql刷题584. 寻找用户推荐人
Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
SQL question brushing 1050 Actors and directors who have worked together at least three times
Germany if was crowned with many awards. How strong is this pair of headphones? In depth evaluation of yinpo GTW 270 hybrid
机器学习11-聚类,孤立点判别
数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
判断链表是否是回文链表