-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberOfIslands.java
65 lines (53 loc) · 1.32 KB
/
NumberOfIslands.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package matrix;
import java.util.*;
/**
* @author Shogo Akiyama
* Solved on 10/04/2019
*
* 200. Number of Islands
* https://leetcode.com/problems/number-of-islands/
* Difficulty: Medium
*
* Approach: DFS
* Runtime: 24 ms, faster than 5.25% of Java online submissions for Number of Islands.
* Memory Usage: 42.2 MB, less than 21.40% of Java online submissions for Number of Islands.
*
* @see MatrixTest#testNumberOfIslands()
*/
public class NumberOfIslands {
private char[][] board;
private int h;
private int w;
private Set<String> seen = new HashSet<String>();;
private int[][] xys = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
board = grid;
h = grid.length;
w = grid[0].length;
int count = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (!seen.contains(i + "-" + j)) {
if (board[i][j] == '1') {
count++;
find(i, j);
}
}
}
}
return count;
}
private void find(int y, int x) {
seen.add(y + "-" + x);
for (int i = 0; i < xys.length; i++) {
int ny = y + xys[i][0];
int nx = x + xys[i][1];
if (ny >= 0 && ny < h && nx >= 0 && nx < w && !seen.contains(ny + "-" + nx) && board[ny][nx] == '1') {
find(ny, nx);
}
}
}
}