-
Notifications
You must be signed in to change notification settings - Fork 0
/
200.Number_of_Islands.java
45 lines (45 loc) · 1.58 KB
/
200.Number_of_Islands.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
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public int numIslands(char[][] grid) {
int rows = grid.length;
int cols = grid[0].length;
boolean[][] visited = new boolean[rows][cols];
int islands = 0;
for (int r=0; r<rows; r++) {
for (int c=0; c<cols; c++) {
if (grid[r][c]=='1' && !visited[r][c]) {
islands++;
// bfs(r, c, visited, grid);
dfs(r, c, visited, grid);
}
}
}
return islands;
}
private void bfs(int row, int col, boolean[][] visited, char[][] grid) {
Queue<Integer[]> queue = new LinkedList<>();
queue.add(new Integer[]{row, col});
while (queue.size() > 0) {
Integer[] top = queue.remove();
int r = top[0];
int c = top[1];
if (r<0 || r>=grid.length || c<0 || c>= grid[0].length || visited[r][c] || grid[r][c]=='0') {
continue;
}
visited[r][c] = true;
queue.add(new Integer[]{r+1, c});
queue.add(new Integer[]{r-1, c});
queue.add(new Integer[]{r, c+1});
queue.add(new Integer[]{r, c-1});
}
}
private void dfs(int r, int c, boolean[][] visited, char[][] grid) {
if (r<0 || r>=grid.length || c<0 || c>= grid[0].length || visited[r][c] || grid[r][c]=='0') {
return;
}
visited[r][c] = true;
dfs(r+1, c, visited, grid);
dfs(r-1, c, visited, grid);
dfs(r, c+1, visited, grid);
dfs(r, c-1, visited, grid);
}
}