forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxAreaOfIsland.java
More file actions
28 lines (25 loc) · 784 Bytes
/
MaxAreaOfIsland.java
File metadata and controls
28 lines (25 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class MaxAreaOfIsland {
public int maxAreaOfIsland(int[][] grid) {
int[] area = new int[1];
int max = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
area[0] = 0;
dfs(grid, i, j, area);
max = Math.max(max, area[0]);
}
}
return max;
}
private void dfs(int[][] grid, int i, int j, int[] count) {
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != 1) {
return;
}
grid[i][j] = 0;
count[0]++;
dfs(grid, i + 1, j, count);
dfs(grid, i - 1, j, count);
dfs(grid, i, j + 1, count);
dfs(grid, i, j - 1, count);
}
}