当前位置:网站首页>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;
}
}
边栏推荐
- There is a new breakthrough in quantum field: the duration of quantum state can exceed 5 seconds
- Petrv2: a unified framework for 3D perception of multi camera images
- 如何写出好代码 — 防御式编程指南
- 剑指 Offer II 105. 岛屿的最大面积
- SQL注入漏洞(Mysql与MSSQL特性)
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- 中国超高分子量聚乙烯产业调研与投资前景报告(2022版)
- In depth Research Report on China's disposable sanitary products production equipment industry (2022 Edition)
- Soft test software designer full truth simulation question (including answer analysis)
- (27) Open operation, close operation, morphological gradient, top hat, black hat
猜你喜欢
SQL injection vulnerability (MySQL and MSSQL features)
【C补充】【字符串】按日期排序显示一个月的日程
LeetCode中等题之TinyURL 的加密与解密
There is a new breakthrough in quantum field: the duration of quantum state can exceed 5 seconds
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
机器学习11-聚类,孤立点判别
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
可迭代对象与迭代器、生成器的区别与联系
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
How to use etcd to realize distributed /etc directory
随机推荐
The difference between the lazy mode of singleton mode and the evil mode
(十七)DAC转换实验
Redis 分布式鎖
为什么你要考虑使用Prisma
(12) About time-consuming printing
Shenyu gateway development: enable and run locally
PHP implements sensitive word filtering system "suggestions collection"
存在安全隐患 起亚召回部分K3新能源
PHP实现敏感词过滤系统「建议收藏」
开发那些事儿:EasyCVR集群设备管理页面功能展示优化
智能运维实战:银行业务流程及单笔交易追踪
[C language supplement] judge which day tomorrow is (tomorrow's date)
[wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
Develop those things: easycvr cluster device management page function display optimization
Reflective XSS vulnerability
在MeterSphere接口测试中如何使用JMeter函数和MockJS函数
Kia recalls some K3 new energy with potential safety hazards
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
越来越多地使用 SLO 来实现可观测性|DevOps
Jojogan practice