-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIslandCount2.java
More file actions
33 lines (28 loc) · 804 Bytes
/
IslandCount2.java
File metadata and controls
33 lines (28 loc) · 804 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
public class IslandCount2 {
public int numIslands(char[][] grid) {
int n = grid.length;
if(n == 0) return 0;
int m = grid[0].length;
if(m == 0) return 0;
int count = 0;
for(int i=0; i < n; i++){
for(int j=0; j < m; j++){
if(grid[i][j] == '1'){
count++;
dfsMark(grid, i, j);
}
}
}
return count;
}
public void dfsMark(char[][] grid, int i, int j){
if(i < 0 || j < 0 || i > grid.length-1 || j > grid[0].length-1 || grid[i][j] != '1'){
return;
}
grid[i][j] = '*';
dfsMark(grid, i+1, j);
dfsMark(grid, i-1, j);
dfsMark(grid, i, j+1);
dfsMark(grid, i, j-1);
}
}