当前位置:网站首页>剑指 Offer II 105. 岛屿的最大面积
剑指 Offer II 105. 岛屿的最大面积
2022-07-01 16:28:00 【彼淇梁】
剑指 Offer II 105. 岛屿的最大面积【中等题】
思路:【DFS】
每遇到一个岛,记录岛屿数量为1,同时以这个岛为中心向上下左右四个方向搜索相邻岛屿并记录岛屿数量,同时将这个岛炸沉(置为0),表示已搜索过。
当一个岛四个方向搜索完毕,更新最大岛屿数量,由于每个岛屿面积一样,所以最大岛屿数量即最大岛屿面积。
代码:
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;
}
}
边栏推荐
- UML旅游管理系统「建议收藏」
- Virtual serial port simulator and serial port debugging assistant tutorial "suggestions collection"
- Dataframe gets the number of words in the string
- Is the programmer's career really short?
- Tutorial on the principle and application of database system (005) -- Yum offline installation of MySQL 5.7 (Linux Environment)
- Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
- 独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
- Concatenate strings to get the result with the smallest dictionary order
- Internet News: "20220222" get together to get licenses; Many products of Jimi have been affirmed by consumers; Starbucks was fined for using expired ingredients in two stores
- Go 语言怎么优化重复的 if err != nil 样板代码?
猜你喜欢

数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)

SystemVerilog-结构体(二)

今天14:00 | 港大、北航、耶鲁、清华、加大等15位ICLR一作讲者精彩继续!

Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation

毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?

Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!

【直播预约】数据库OBCP认证全面升级公开课

Buuctf gold III

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"

Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
随机推荐
How does go use symmetric encryption?
你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费
China BMS battery management system Market Research Report (2022 Edition)
Go 语言怎么使用对称加密?
判断一棵二叉树是否为平衡二叉树
模板引擎Velocity 基礎
How to use phpipam to manage IP addresses and subnets
FPN网络详解
Redis6.0 new features
How to use F1 to F12 correctly on laptop keyboard
单例模式的懒汉模式跟恶汉模式的区别
UML旅游管理系统「建议收藏」
[observation] where is the consulting going in the digital age? Thoughts and actions of softcom consulting
Redis Distributed Lock
What are the differences between PHP and DW
Germany if was crowned with many awards. How strong is this pair of headphones? In depth evaluation of yinpo GTW 270 hybrid
软件工程导论——第六章——详细设计
Girls who want to do software testing look here
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
How to restore the system with one click on Lenovo laptop