当前位置:网站首页>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
边栏推荐
- 22. Bracket generation
- Van uploader upload picture implementation process, using native input to upload pictures
- [51 single chip microcomputer traffic light simulation]
- GIS practical tips (III) - how to add legend in CASS?
- Wonderful review | the sixth issue of "source" - open source economy and industrial investment
- Pyqt5 store opencv pictures into the built-in sqllite database and query
- PC starts multiple wechat at one time
- Embrace the new blue ocean of machine vision and hope to open a new "Ji" encounter for the development of digital economy
- "Play with Lighthouse" lightweight application server self built DNS resolution server
- AUTOSAR from introduction to mastery 100 lectures (86) - 2F of UDS service foundation
猜你喜欢

Important knowledge of transport layer (interview, retest, final)

filter()遍历Array异常友好

初鉴canvas,展示个小小的小案例

解决虚拟机中Oracle每次要设置ip的问题

Read the data in Presto through sparksql and save it to Clickhouse
![[51 single chip microcomputer traffic light simulation]](/img/70/0d78e38c49ce048b179a85312d063f.png)
[51 single chip microcomputer traffic light simulation]

100 lectures on practical application cases of Excel (VIII) - report connection function of Excel

Embrace the new blue ocean of machine vision and hope to open a new "Ji" encounter for the development of digital economy

1130 - host XXX is not allowed to connect to this MySQL server error in Navicat remote connection database

Wonderful review | the sixth issue of "source" - open source economy and industrial investment
随机推荐
Metalama简介4.使用Fabric操作项目或命名空间
7_ The cell type scores obtained by addmodule and gene addition method are compared in space
31. 下一个排列
How to click an object to play an animation
AUTOSAR from introduction to mastery 100 lectures (86) - 2F of UDS service foundation
Conflict between Mui picker and drop-down refresh
Go iris framework implements multi service Demo: start (listen to port 8084) service 2 through the interface in service 1 (listen to port 8083)
Melt reshape decast long data short data length conversion data cleaning row column conversion
Free and open source agricultural Internet of things cloud platform (version: 3.0.1)
The El table horizontal scroll bar is fixed at the bottom of the visual window
Record the problems encountered in using v-print
Recovering data with MySQL binlog
Servlet监听器&过滤器介绍
4.22 study record (you only did water problems in one day, didn't you)
SSM整合之pom.xml
Custom nail robot alarm
The quill editor image zooms, multiple rich text boxes are used on one page, and the quill editor upload image address is the server address
初鉴canvas,展示个小小的小案例
Complete project data of UAV apriltag dynamic tracking landing based on openmv (LabVIEW + openmv + apriltag + punctual atom four axes)
About the 'enum' enumeration type and structure.