给定一个包含了一些 0
和 1
的非空二维数组 grid
。
一个 岛屿 是由一些相邻的 1
(代表土地) 构成的组合,这里的「相邻」要求两个 1
必须在水平或者竖直方向上相邻。你可以假设 grid
的四个边缘都被 0
(代表水)包围着。
找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为 0
。)
示例 1:
[[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:
[[0,0,0,0,0,0,0,0]]
对于上面这个给定的矩阵, 返回 0
。
注意: 给定的矩阵grid
的长度和宽度都不超过 50。
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
def dfs(grid, i, j, m, n):
if i < 0 or i >= m or j < 0 or j >= n or grid[i][j] == 0:
return 0
grid[i][j] = 0
res = 1
for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
res += dfs(grid, i + x, j + y, m, n)
return res
m, n = len(grid), len(grid[0])
res = 0
for i in range(m):
for j in range(n):
t = dfs(grid, i, j, m, n)
res = max(res, t)
return res
class Solution {
private int[][] directions = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
public int maxAreaOfIsland(int[][] grid) {
int m = grid.length, n = grid[0].length;
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int t = dfs(grid, i, j, m, n);
res = Math.max(res, t);
}
}
return res;
}
private int dfs(int[][] grid, int i, int j, int m, int n) {
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
return 0;
}
grid[i][j] = 0;
int res = 1;
for (int[] direction : directions) {
res += dfs(grid, i + direction[0], j + direction[1], m, n);
}
return res;
}
}
function maxAreaOfIsland(grid: number[][]): number {
let m = grid.length, n = grid[0].length;
let res = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
res = Math.max(dfs(grid, i, j), res);
}
}
}
return res;
};
function dfs(grid: number[][], i: number, j: number): number {
let m = grid.length, n = grid[0].length;
if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || grid[i][j] == 0) {
return 0;
}
grid[i][j] = 0;
let res = 1;
for (let [dx, dy] of [[0, 1], [0, -1], [1, 0], [-1, 0]]) {
res += dfs(grid, i + dx, j + dy);
}
return res;
}
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int t = dfs(grid, i, j, m, n);
res = max(res, t);
}
}
return res;
}
private:
vector<vector<int>> directions = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
int dfs(vector<vector<int>>& grid, int i, int j, int m, int n) {
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
return 0;
}
grid[i][j] = 0;
int res = 1;
for (auto direction : directions) {
res += dfs(grid, i + direction[0], j + direction[1], m, n);
}
return res;
}
};