当前位置:网站首页>LeetCode_DFS_中等_695.岛屿的最大面积
LeetCode_DFS_中等_695.岛屿的最大面积
2022-04-23 13:07:00 【一瓢江湖我沉浮】
目录
1.题目
给你一个大小为 m x n 的二进制矩阵 grid 。
岛屿 是由一些相邻的 1 (代表土地) 构成的组合,这里的「相邻」要求两个 1 必须在 水平或者竖直的四个方向上 相邻。你可以假设 grid 的四个边缘都被 0(代表水)包围着。
岛屿的面积是岛上值为 1 的单元格的数目。
计算并返回 grid 中最大的岛屿面积。如果没有岛屿,则返回面积为 0 。
示例 1:

输入:grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
输出:6
解释:答案不应该是 11 ,因为岛屿只能包含水平或垂直这四个方向上的 1 。
示例 2:
输入:grid = [[0,0,0,0,0,0,0,0]]
输出:0
提示:
m == grid.length
n == grid[i].length
1 <= m, n <= 50
grid[i][j] 为 0 或 1
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/max-area-of-island
2.思路
(1)DFS
本题实际上是LeetCode_DFS_中等_200.岛屿数量这题的变形,只不过 dfs 函数淹没岛屿的同时,要记录这个岛屿的面积。
3.代码实现(Java)
//思路1————DFS
class Solution {
public int maxAreaOfIsland(int[][] grid) {
//res记录岛屿的最大面积,初始值为0
int res = 0;
int m = grid.length;
int n = grid[0].length;
//遍历grid
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
//淹没该岛屿,并且更新res
res = Math.max(res, dfs(grid, i, j));
}
}
}
return res;
}
//淹没与(i, j)相邻的陆地,并且返回被淹没的陆地面积
public int dfs(int[][] grid, int i, int j) {
int m = grid.length;
int n = grid[0].length;
//检查边界条件
if (i < 0 || j < 0 || i >= m || j >= n) {
return 0;
}
if (grid[i][j] == 0) {
//该位置是海水,直接返回
return 0;
}
//将(i, j)变成海水
grid[i][j] = 0;
return dfs(grid, i - 1, j) +
dfs(grid, i + 1, j) +
dfs(grid, i, j - 1) +
dfs(grid, i, j + 1) + 1;
}
}
版权声明
本文为[一瓢江湖我沉浮]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43004044/article/details/124359093
边栏推荐
- AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters
- Async void caused the program to crash
- AUTOSAR from introduction to mastery 100 lectures (50) - AUTOSAR memory management series - ECU abstraction layer and MCAL layer
- 将新增和编辑的数据同步更新到列表
- CVPR 2022 & ntire 2022 | the first transformer for hyperspectral image reconstruction
- mui picker和下拉刷新冲突问题
- 4.22学习记录(你一天只做了水题是吗)
- Office 2021 installation package download and activation tutorial
- Proteus 8.10 installation problem (personal test is stable and does not flash back!)
- Packet capturing and sorting -- TCP protocol [8]
猜你喜欢

The filter() traverses the array, which is extremely friendly

31. Next arrangement

MySQL supports IP access

CVPR 2022 & ntire 2022 | the first transformer for hyperspectral image reconstruction

Nodejs + Mysql realize simple registration function (small demo)

About the 'enum' enumeration type and structure.

Free and open source intelligent charging pile SaaS cloud platform of Internet of things

Free and open source charging pile Internet of things cloud platform

Custom nail robot alarm

初鉴canvas,展示个小小的小案例
随机推荐
Pyqt5 store opencv pictures into the built-in sqllite database and query
The El table horizontal scroll bar is fixed at the bottom of the visual window
AUTOSAR from introduction to mastery 100 lectures (50) - AUTOSAR memory management series - ECU abstraction layer and MCAL layer
Office 2021 installation package download and activation tutorial
Synchronously update the newly added and edited data to the list
Connect orcale
Introducing vant components on demand
nodeJs + websocket 循环小案例
melt reshape decast 长数据短数据 长短转化 数据清洗 行列转化
Read the data in Presto through sparksql and save it to Clickhouse
Packet capturing and sorting -- TCP protocol [8]
Translation of multi modal visual tracking: review and empirical comparison
22. 括号生成
GIS practical tips (III) - how to add legend in CASS?
STM32 is connected to the motor drive, the DuPont line supplies power, and then the back burning problem
HQL find the maximum value in a range
After the data of El table is updated, the data in the page is not updated this$ Forceupdate() has no effect
Free and open source charging pile Internet of things cloud platform
22. Bracket generation
Customize classloader and implement hot deployment - use loadclass