Skip to content

Commit ad744b1

Browse files
author
houfachao
committed
695. 岛屿的最大面积
1 parent a7daafb commit ad744b1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.geekidentity.leetcode.n0695;
2+
3+
public class MaxAreaOfIsland {
4+
public int maxAreaOfIsland(int[][] grid) {
5+
int maxArea = 0;
6+
for (int row = 0; row < grid.length; row++) {
7+
for (int col = 0; col < grid[0].length; col++) {
8+
if (grid[row][col] == 1) {
9+
int area = area(grid, row, col);
10+
maxArea = Math.max(area, maxArea);
11+
}
12+
}
13+
}
14+
return maxArea;
15+
}
16+
17+
public int area(int[][] grid, int row, int col) {
18+
if (!inArea(grid, row, col)) return 0;
19+
if (grid[row][col] != 1) return 0;
20+
grid[row][col] = 2;
21+
return 1 +
22+
area(grid, row - 1, col) +
23+
area(grid, row + 1, col) +
24+
area(grid, row, col - 1) +
25+
area(grid, row, col + 1);
26+
}
27+
28+
public boolean inArea(int[][] grid, int row, int col) {
29+
return 0 <= row && row < grid.length && 0 <= col && col < grid[0].length;
30+
}
31+
}

0 commit comments

Comments
 (0)