695:岛屿的最大面积
This commit is contained in:
parent
825e7887bf
commit
c3d57a5143
75
src/main/java/leetcode/editor/cn/MaxAreaOfIsland.java
Normal file
75
src/main/java/leetcode/editor/cn/MaxAreaOfIsland.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
//给定一个包含了一些 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)
|
||||||
|
|
||||||
|
}
|
32
src/main/java/leetcode/editor/cn/MaxAreaOfIsland.md
Normal file
32
src/main/java/leetcode/editor/cn/MaxAreaOfIsland.md
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<p>给定一个包含了一些 <code>0</code> 和 <code>1</code> 的非空二维数组 <code>grid</code> 。</p>
|
||||||
|
|
||||||
|
<p>一个 <strong>岛屿</strong> 是由一些相邻的 <code>1</code> (代表土地) 构成的组合,这里的「相邻」要求两个 <code>1</code> 必须在水平或者竖直方向上相邻。你可以假设 <code>grid</code> 的四个边缘都被 <code>0</code>(代表水)包围着。</p>
|
||||||
|
|
||||||
|
<p>找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为 <code>0</code> 。)</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>[[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,<strong>1</strong>,0,<strong>1</strong>,0,0],
|
||||||
|
[0,1,0,0,1,1,0,0,<strong>1</strong>,<strong>1</strong>,<strong>1</strong>,0,0],
|
||||||
|
[0,0,0,0,0,0,0,0,0,0,<strong>1</strong>,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]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p>对于上面这个给定矩阵应返回 <code>6</code>。注意答案不应该是 <code>11</code> ,因为岛屿只能包含水平或垂直的四个方向的 <code>1</code> 。</p>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>[[0,0,0,0,0,0,0,0]]</pre>
|
||||||
|
|
||||||
|
<p>对于上面这个给定的矩阵, 返回 <code>0</code>。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>注意: </strong>给定的矩阵<code>grid</code> 的长度和宽度都不超过 50。</p>
|
||||||
|
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>并查集</li><li>数组</li><li>矩阵</li></div></div>\n<div><li>👍 505</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user