当前位置:网站首页>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;
}
}
边栏推荐
- Detailed explanation of string's trim() and substring()
- String class
- mysql -- explain性能优化
- Computed property “xxx“ was assigned to but it has no setter.
- Redis 分布式鎖
- SystemVerilog structure (II)
- 反射型XSS漏洞
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- Replace UUID, nanoid is faster and safer!
- 在MeterSphere接口测试中如何使用JMeter函数和MockJS函数
猜你喜欢

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"

String的trim()和substring()详解

Vulnhub range hacker_ Kid-v1.0.1

6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单

vulnhub靶场-hacksudo - Thor

Detailed explanation of string's trim() and substring()

(16) ADC conversion experiment

Soft test network engineer full truth simulation question (including answer and analysis)

How to use etcd to realize distributed /etc directory

Redis 分布式鎖
随机推荐
JDBC: deep understanding of Preparedstatement and statement[easy to understand]
Please, stop painting star! This has nothing to do with patriotism!
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
How to use etcd to realize distributed /etc directory
Machine learning 11 clustering, outlier discrimination
越来越多地使用 SLO 来实现可观测性|DevOps
Object. fromEntries()
反射型XSS漏洞
MySQL learning summary
Reflective XSS vulnerability
Detailed explanation of string's trim() and substring()
Enter wechat applet
SQL injection vulnerability (MySQL and MSSQL features)
求求你们,别再刷 Star 了!这跟“爱国”没关系!
In aks, use secret in CSI driver mount key vault
Research Report on development prediction and investment direction of nylon filament sewing thread in China (2022 Edition)
线上开通ETF基金账户安全吗?有哪些步骤?
Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open
Concatenate strings to get the result with the smallest dictionary order
中国冰淇淋市场深度评估及发展趋势预测报告(2022版)