当前位置:网站首页>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;
}
}
边栏推荐
- Vulnhub range hacksudo Thor
- Machine learning 11 clustering, outlier discrimination
- 中国锦纶长丝缝纫线发展预测与投资方向研究报告(2022版)
- Research Report on development monitoring and investment prospects of China's smart environmental protection industry (2022 Edition)
- 中国超高分子量聚乙烯产业调研与投资前景报告(2022版)
- LeetCode中等题之TinyURL 的加密与解密
- ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
- Is it safe to open an ETF account online? What are the steps?
- 如何写出好代码 — 防御式编程指南
- 深度优先遍历和广度优先遍历[通俗易懂]
猜你喜欢
重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
PETRv2:一个多摄像头图像3D感知的统一框架
[pyg] document summary and project experience (continuously updated
英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
How to use JMeter function and mockjs function in metersphere interface test
Babbitt | yuan universe daily must read: Naixue coin, Yuan universe paradise, virtual stock game Do you understand Naixue's tea's marketing campaign of "operation pull full"
How wild are hackers' ways of making money? CTF reverse entry Guide
Detailed explanation of string's trim() and substring()
LeetCode中等题之TinyURL 的加密与解密
(十六)ADC转换实验
随机推荐
Code example of libcurl download file
两数之和c语言实现[通俗易懂]
如何使用 etcd 实现分布式 /etc 目录
Machine learning 11 clustering, outlier discrimination
Redis6.0 new features
In depth evaluation and development trend prediction report of China's ice cream market (2022 Edition)
存在安全隐患 起亚召回部分K3新能源
Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
Girls who want to do software testing look here
Radhat builds intranet Yum source server
RadHat搭建内网YUM源服务器
拼接字符串,得到字典序最小的结果
The amazing open source animation library is not only awesome, but also small
Yyds dry inventory MySQL RC transaction isolation level implementation
SQL injection vulnerability (MySQL and MSSQL features)
Kia recalls some K3 new energy with potential safety hazards
《中国智慧环保产业发展监测与投资前景研究报告(2022版)》
(12) About time-consuming printing
【C补充】【字符串】按日期排序显示一个月的日程
[Supplément linguistique c] déterminer quel jour est demain (date de demain)