当前位置:网站首页>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;
}
}
边栏推荐
- 【C語言補充】判斷明天是哪一天(明天的日期)
- Basic usage of Frida
- [pyg] document summary and project experience (continuously updated
- China sorbitol Market Forecast and investment strategy report (2022 Edition)
- 剑指 Offer II 105. 岛屿的最大面积
- 剑指 Offer 20. 表示数值的字符串
- Is it safe to open a stock account by mobile phone? What do you need to bring with you to open an account?
- Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
- Petrv2: a unified framework for 3D perception of multi camera images
- (16) ADC conversion experiment
猜你喜欢
![[Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①](/img/24/23f6534e2c74724f9512c5b18661b6.png)
[Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①

Machine learning 11 clustering, outlier discrimination

机器学习11-聚类,孤立点判别

Gameframework eating guide

Official announcement! Hong Kong University of science and Technology (Guangzhou) approved!

How to use JMeter function and mockjs function in metersphere interface test

DNS

为什么你要考虑使用Prisma

在MeterSphere接口测试中如何使用JMeter函数和MockJS函数

走进微信小程序
随机推荐
Code example of libcurl download file
【C补充】【字符串】按日期排序显示一个月的日程
Countdownlatch blocking wait for multithreading concurrency
Yyds dry inventory MySQL RC transaction isolation level implementation
多线程使用不当导致的 OOM
走进微信小程序
[Supplément linguistique c] déterminer quel jour est demain (date de demain)
ShenYu 网关开发:在本地启用运行
【C语言补充】判断明天是哪一天(明天的日期)
(12) About time-consuming printing
中国PBAT树脂市场预测及战略研究报告(2022版)
如何使用 etcd 实现分布式 /etc 目录
中国酶制剂市场预测与投资战略研究报告(2022版)
String class
(十七)DAC转换实验
Openlayers 自定义气泡框以及定位到气泡框
redis -- 数据类型及操作
Alibaba cloud Li Feifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
Mysql database - Advanced SQL statement (2)
【C语言基础】12 字符串