-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumberOfIslands.java
More file actions
51 lines (41 loc) · 1.72 KB
/
NumberOfIslands.java
File metadata and controls
51 lines (41 loc) · 1.72 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
// Given a 2d grid map of '1's (land) and '0's (water), count the number of islands.
// An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
// You may assume all four edges of the grid are all surrounded by water.
// See: https://leetcode.com/problems/number-of-islands/
// See: https://leetcode.com/problems/number-of-islands/discuss/583559/Java-Very-Simple-DFS
package leetcode.others;
import java.util.Arrays;
public class NumberOfIslands {
private int[][] directions = new int[][] { {1, 0}, {-1, 0}, {0, -1}, {0, 1} };
public int numIslands(char[][] grid) {
int numOfIslands = 0;
for (int i = 0; i < grid.length; i++)
for (int j = 0; j < grid[0].length; j++)
if (grid[i][j] == '1') {
numOfIslands++;
dfs(grid, i, j);
}
return numOfIslands;
}
private void dfs(char[][] grid, int i, int j) {
grid[i][j] = '0';
for (int[] dir : directions) {
int cI = dir[0] + i;
int cJ = dir[1] + j;
if (cI >= 0 && cI < grid.length && cJ >= 0 && cJ < grid[0].length && grid[cI][cJ] == '1')
dfs(grid, cI, cJ);
}
}
public static void main(String[] args) {
NumberOfIslands sln = new NumberOfIslands();
char[][] grid = new char[][] {
{'1', '1', '0', '0', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '1', '0', '0'},
{'0', '0', '0', '1', '1'}
};
// sln.dfs(grid, 0, 0);
System.out.println(sln.numIslands(grid));
for (char[] row : grid) System.out.println(Arrays.toString(row));
}
}