forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0200.java
33 lines (30 loc) · 873 Bytes
/
0200.java
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
29
30
31
32
33
class Solution {
public int numIslands(char[][] grid) {
if (grid.length == 0) return 0;
r = grid.length;
c = grid[0].length;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (grid[i][j] == '1') {
grid[i][j] = '0';
res++;
dfs(grid, i, j);
}
}
}
return res;
}
private int r, c;
private int res = 0;
private int[] d = {0, 1, 0, -1, 0};
private void dfs(char[][] grid, int x, int y) {
for (int i = 0; i < 4; i++) {
int nx = x + d[i];
int ny = y + d[i + 1];
if (0 <= nx && nx < r && 0 <= ny && ny < c && grid[nx][ny] == '1') {
grid[nx][ny] = '0';
dfs(grid, nx, ny);
}
}
}
}