当前位置:网站首页>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;
}
}
边栏推荐
- 如何写出好代码 — 防御式编程指南
- 剑指 Offer II 105. 岛屿的最大面积
- (17) DAC conversion experiment
- Concatenate strings to get the result with the smallest dictionary order
- 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"
- Depth first traversal and breadth first traversal [easy to understand]
- Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
- China acetonitrile market forecast and strategic consulting research report (2022 Edition)
- [C language foundation] 12 strings
- Enter wechat applet
猜你喜欢
Vulnhub range hacker_ Kid-v1.0.1
走进微信小程序
[Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①
Vulnhub range hacksudo Thor
Why should you consider using prism
【Try to Hack】vulnhub DC4
为什么你要考虑使用Prisma
(28) Shape matching based on contour features
PETRv2:一个多摄像头图像3D感知的统一框架
Gold, silver and four want to change jobs, so we should seize the time to make up
随机推荐
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
为什么你要考虑使用Prisma
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
Oom caused by improper use of multithreading
多线程并发之CountDownLatch阻塞等待
Shenyu gateway development: enable and run locally
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
Hidden Markov model (HMM): model parameter estimation
开发那些事儿:EasyCVR集群设备管理页面功能展示优化
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
期货先锋这个软件正规吗安全吗?选择哪家期货公司更安全?
Transition technology from IPv4 to IPv6
Radhat builds intranet Yum source server
机器学习11-聚类,孤立点判别
Research Report on China's enzyme Market Forecast and investment strategy (2022 Edition)
重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
ACL 2022 | 分解的元学习小样本命名实体识别
Depth first traversal and breadth first traversal [easy to understand]
[C language foundation] 12 strings
There is a new breakthrough in quantum field: the duration of quantum state can exceed 5 seconds