-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC_200
More file actions
44 lines (28 loc) · 789 Bytes
/
Copy pathLC_200
File metadata and controls
44 lines (28 loc) · 789 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public int numIslands(char[][] grid) {
int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == '1') {
count++;
dfs(grid, i, j);
}
}
}
return count;
}
private void dfs(char[][] grid, int row, int col) {
if (row < 0 ||
row >= grid.length ||
col < 0 ||
col >= grid[0].length ||
grid[row][col] == '0') {
return;
}
grid[row][col] = '0';
dfs(grid, row + 1, col);
dfs(grid, row - 1, col);
dfs(grid, row, col + 1);
dfs(grid, row, col - 1);
}
}