当前位置:网站首页>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;
}
}
边栏推荐
- Intelligent operation and maintenance practice: banking business process and single transaction tracking
- Enter wechat applet
- 深度优先遍历和广度优先遍历[通俗易懂]
- ACL 2022 | decomposed meta learning small sample named entity recognition
- Judge whether a binary tree is a balanced binary tree
- There is a new breakthrough in quantum field: the duration of quantum state can exceed 5 seconds
- How to write good code - Defensive Programming Guide
- SystemVerilog structure (II)
- 【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam
- Soft test network engineer full truth simulation question (including answer and analysis)
猜你喜欢

PETRv2:一个多摄像头图像3D感知的统一框架

ShenYu 网关开发:在本地启用运行

多线程并发之CountDownLatch阻塞等待

为什么你要考虑使用Prisma
![[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code](/img/de/1f572c62a0d034da9a8acb5c2f9602.jpg)
[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code

SQL question brushing 1050 Actors and directors who have worked together at least three times

剑指 Offer 20. 表示数值的字符串

Vulnhub range hacksudo Thor

Redis Distributed Lock

存在安全隐患 起亚召回部分K3新能源
随机推荐
Countdownlatch blocking wait for multithreading concurrency
mysql -- explain性能优化
麦趣尔:媒体报道所涉两批次产品已下架封存,受理消费者诉求
PHP implements sensitive word filtering system "suggestions collection"
Redis Distributed Lock
[Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①
中国酶制剂市场预测与投资战略研究报告(2022版)
China sorbitol Market Forecast and investment strategy report (2022 Edition)
FRP intranet penetration, reverse proxy
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
Depth first traversal and breadth first traversal [easy to understand]
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Oom caused by improper use of multithreading
(28) Shape matching based on contour features
Official announcement! Hong Kong University of science and Technology (Guangzhou) approved!
Pytest learning notes (13) -allure of allure Description () and @allure title()
ACL 2022 | decomposed meta learning small sample named entity recognition
Soft test network engineer full truth simulation question (including answer and analysis)
Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
(十六)ADC转换实验