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.

Example 1:

1
2
3
4
11110
11010
11000
00000

Answer: 1

Example 2:

11000
11000
00100
00011

Answer: 3

DFS

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
public class Solution {
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) return 0;
int count = 0;
int n = grid.length, m = grid[0].length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == '1') {
DFS(grid, i, j, n, m);
count++;
}
}
}
return count;
}

public void DFS(char[][] grid, int i, int j, int n, int m) {
if (i >= 0 && j >= 0 && i < n && j < m && grid[i][j] != '0') {
grid[i][j] = '0';
DFS(grid, i, j - 1, n, m);
DFS(grid, i, j + 1, n, m);
DFS(grid, i - 1, j, n, m);
DFS(grid, i + 1, j, n, m);
}
}
}

BFS

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
public class Solution {
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) return 0;
int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == '1') {
mark(grid, i, j);
count++;
}
}
}
return count;
}

public void mark(char[][] grid, int i, int j) {
if (i >= 0 && j >= 0 && i < grid.length && j < grid[i].length && grid[i][j] == '1') {
grid[i][j] = '0';
int n = grid.length;
int m = grid[0].length;
Queue<Integer> queue = new LinkedList<>();
int position = i * m + j;
queue.offer(position);

while (! queue.isEmpty()) {
position = queue.poll();
int x = position / m;
int y = position % m;
BFS(grid, x - 1, y, queue);
BFS(grid, x + 1, y, queue);
BFS(grid, x, y - 1, queue);
BFS(grid, x, y + 1, queue);
}
}
}

public void BFS(char[][] grid, int i, int j, Queue queue) {
if (i >= 0 && j >= 0 && i < grid.length && j < grid[i].length && grid[i][j] == '1') {
queue.offer(i * grid[i].length + j);
grid[i][j] = '0';
}
}
}

Union Find solution

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
60
61
62
63
64
65
66
67
68
public class Solution {
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) return 0;
int n = grid.length, m = grid[0].length;
UnionFind uf = new UnionFind(grid, m, n);

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == '1') {
int curPosition = i * m + j;
int nextPosition;
if (i > 0 && grid[i - 1][j] == '1') {
nextPosition = curPosition - m;
uf.union(curPosition, nextPosition);
}
if (i < n - 1 && grid[i + 1][j] == '1') {
nextPosition = curPosition + m;
uf.union(curPosition, nextPosition);
}
if (j > 0 && grid[i][j - 1] == '1') {
nextPosition = curPosition - 1;
uf.union(curPosition, nextPosition);
}
if (j < m - 1 && grid[i][j + 1] == '1') {
nextPosition = curPosition + 1;
uf.union(curPosition, nextPosition);
}
}
}
}

return uf.count;
}

public class UnionFind {
int count =0;
int[] id;

public UnionFind(char[][] grid, int m, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == '1') count++;
}
}
id = new int[n * m];
for (int i = 0; i < m * n; i++) {
id[i] = i;
}
}

public int find(int p) {
while (id[p] != p) {
id[p] = id[id[p]];
p = id[p];
}
return p;
}

public void union(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (qRoot != pRoot) {
id[pRoot] = qRoot;
count--;
}
}
}
}