-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfIslands.java
More file actions
59 lines (48 loc) · 1.52 KB
/
Copy pathNumberOfIslands.java
File metadata and controls
59 lines (48 loc) · 1.52 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
200. 岛屿数量
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
示例 1:
输入:
11110
11010
11000
00000
输出: 1
示例 2:
输入:
11000
11000
00100
00011
输出: 3
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-islands
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
package graph;
public class NumberOfIslands {
static int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public static int numIslands(int[][] grid) {
boolean[][] visited = new boolean[grid.length][grid[0].length];
int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (!visited[i][j] && grid[i][j] == 1) {
count++;
dfs(grid, visited, i, j);
}
}
}
return count;
}
private static void dfs(int[][] grid, boolean[][] visited, int x, int y) {
visited[x][y] = true;
for (int[] pair : dirs) {
int nx = x + pair[0];
int ny = y + pair[1];
if (nx >= 0 && nx < grid.length && ny >= 0 && ny < grid[0].length && !visited[nx][ny] && grid[nx][ny] == 1) {
dfs(grid, visited, nx, ny);
}
}
}
}