当前位置:网站首页>Sword finger offer II 105 Maximum area of the island
Sword finger offer II 105 Maximum area of the island
2022-07-01 17:31:00 【PI Qiliang】
The finger of the sword Offer II 105. The largest area of the island 【 Medium question 】
Ideas :【DFS】
Every time I meet an island , Record the number of islands as 1, At the same time, take this island as the center, search the adjacent islands in four directions up, down, left and right, and record the number of islands , At the same time, the island was sunk ( Set as 0), Indicates that it has been searched .
When an island is searched in four directions , Update the maximum number of islands , Because each island has the same area , So the largest number of islands is the largest island area .
Code :
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;
}
}
边栏推荐
- The amazing open source animation library is not only awesome, but also small
- [wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
- ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
- [C language supplement] judge which day tomorrow is (tomorrow's date)
- [C language foundation] 12 strings
- Soft test software designer full truth simulation question (including answer analysis)
- 拼接字符串,得到字典序最小的结果
- 中国超高分子量聚乙烯产业调研与投资前景报告(2022版)
- 整形数组合并【JS】
- PHP implements sensitive word filtering system "suggestions collection"
猜你喜欢

Redis distributed lock

Girls who want to do software testing look here

Intelligent operation and maintenance practice: banking business process and single transaction tracking
![[C language foundation] 12 strings](/img/42/9c024eb08eb935fe66c3aaac7589d8.jpg)
[C language foundation] 12 strings
![[pyg] document summary and project experience (continuously updated](/img/b4/75da8c3e657069be4e3e3bfd5b2dc0.png)
[pyg] document summary and project experience (continuously updated

Sword finger offer 20 String representing numeric value

GameFramework食用指南

Redis6.0 new features

String的trim()和substring()详解

官宣!香港科技大学(广州)获批!
随机推荐
Roewe rx5's "a little more" product strategy
(1) CNN network structure
Openlayers customize bubble boxes and navigate to bubble boxes
Is it safe to open a stock account by mobile phone? What do you need to bring with you to open an account?
中国PBAT树脂市场预测及战略研究报告(2022版)
RadHat搭建内网YUM源服务器
libcurl下载文件的代码示例
《中国智慧环保产业发展监测与投资前景研究报告(2022版)》
多线程使用不当导致的 OOM
ACL 2022 | decomposed meta learning small sample named entity recognition
Report on research and investment prospects of China's silicon nitride ceramic substrate industry (2022 Edition)
[pyg] document summary and project experience (continuously updated
在MeterSphere接口测试中如何使用JMeter函数和MockJS函数
GameFramework食用指南
China sorbitol Market Forecast and investment strategy report (2022 Edition)
多线程并发之CountDownLatch阻塞等待
Why should you consider using prism
提交review时ReviewBoard出现500错误解决方法
Redis 分布式鎖
Integer array merge [JS]