695. Max Area of Island (M)
https://leetcode.com/problems/max-area-of-island/
Last updated
https://leetcode.com/problems/max-area-of-island/
Last updated
You are given an m x n
binary matrix grid
. An island is a group of 1
's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
The area of an island is the number of cells with a value 1
in the island.
Return the maximum area of an island in grid
. If there is no island, return 0
.
Example 1:
Example 2:
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 50
grid[i][j]
is either 0
or 1
.
这题的大体思路和之前完全一样,只不过 dfs
函数淹没岛屿的同时,还应该想办法记录这个岛屿的面积。
我们可以给 dfs
函数设置返回值,记录每次淹没的陆地的个数,直接看解法吧:
解法和之前相比差不多,