75 lines
2.3 KiB
Java
75 lines
2.3 KiB
Java
//给定一个包含了一些 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。
|
||
// Related Topics 深度优先搜索 广度优先搜索 并查集 数组 矩阵
|
||
// 👍 505 👎 0
|
||
|
||
package leetcode.editor.cn;
|
||
|
||
//695:岛屿的最大面积
|
||
class MaxAreaOfIsland {
|
||
public static void main(String[] args) {
|
||
//测试代码
|
||
Solution solution = new MaxAreaOfIsland().new Solution();
|
||
}
|
||
|
||
//力扣代码
|
||
//leetcode submit region begin(Prohibit modification and deletion)
|
||
class Solution {
|
||
public int maxAreaOfIsland(int[][] grid) {
|
||
int max = 0;
|
||
for (int i = 0; i < grid.length; i++) {
|
||
for (int j = 0; j < grid[0].length; j++) {
|
||
if (grid[i][j] == 1) {
|
||
max = Math.max(max, dfs(grid, i, j));
|
||
}
|
||
}
|
||
}
|
||
return max;
|
||
}
|
||
|
||
private int dfs(int[][] grid, int x, int y) {
|
||
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] == 0) {
|
||
return 0;
|
||
}
|
||
grid[x][y]=0;
|
||
int count = 1;
|
||
count += dfs(grid, x - 1, y);
|
||
count += dfs(grid, x + 1, y);
|
||
count += dfs(grid, x, y - 1);
|
||
count += dfs(grid, x, y + 1);
|
||
return count;
|
||
}
|
||
}
|
||
//leetcode submit region end(Prohibit modification and deletion)
|
||
|
||
} |