Skip to content

Commit 9b9472c

Browse files
edit 200
1 parent b6fd947 commit 9b9472c

File tree

5 files changed

+172
-160
lines changed

5 files changed

+172
-160
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ Your ideas/fixes/algorithms are more than welcome!
408408
|203|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_203.java)| O(n)|O(1) | Easy
409409
|202|[Happy Number](https://leetcode.com/problems/happy-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_202.java)| O(k)|O(k) | Easy
410410
|201|[Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_201.java)| O(min(m,n))|O(1) | Medium | Bit Manipulation
411-
|200|[Number of Islands](https://leetcode.com/problems/number-of-islands/)|[Union Find](../master/src/main/java/com/fishercoder/solutions/_200UnionFind.java) [DFS](../master/MEDIUM/src/medium/_200DFS.java)| O(m*n)|O(m*n) | Medium| Union Find, DFS
411+
|200|[Number of Islands](https://leetcode.com/problems/number-of-islands/)|[Solution](../master/MEDIUM/src/medium/_200.java)| O(m*n)|O(m*n) | Medium| Union Find, DFS
412412
|199|[Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_199.java)| O(n)|O(n)| Medium | BFS
413413
|198|[House Robber](https://leetcode.com/problems/house-robber/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_198.java)| O(n)|O(n)| Easy | DP
414414
|191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_191.java)| O(n)|O(1)| Easy | Bit Manipulation
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 200. Number of Islands
5+
* Given a 2d grid map of '1's (land) and '0's (water),
6+
* count the number of islands.
7+
* An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
8+
* You may assume all four edges of the grid are all surrounded by water.
9+
10+
Example 1:
11+
12+
11110
13+
11010
14+
11000
15+
00000
16+
Answer: 1
17+
18+
Example 2:
19+
20+
11000
21+
11000
22+
00100
23+
00011
24+
Answer: 3
25+
26+
*/
27+
public class _200 {
28+
29+
public static class DFSSolution {
30+
31+
public int numIslands(char[][] grid) {
32+
if (grid == null || grid.length == 0) return 0;
33+
int count = 0;
34+
int m = grid.length, n = grid[0].length;
35+
for (int i = 0; i < m; i++) {
36+
for (int j = 0; j < n; j++) {
37+
if (grid[i][j] == '1') {
38+
count++;
39+
dfs(grid, i, j, m, n);
40+
}
41+
}
42+
}
43+
return count;
44+
}
45+
46+
void dfs(char[][] grid, int i, int j, int m, int n) {
47+
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') return;
48+
grid[i][j] = '0';
49+
dfs(grid, i + 1, j, m, n);
50+
dfs(grid, i, j + 1, m, n);
51+
dfs(grid, i - 1, j, m, n);
52+
dfs(grid, i, j - 1, m, n);
53+
}
54+
}
55+
56+
public static class UnionFindSolution {
57+
58+
class UnionFind{
59+
int count;
60+
int m, n;
61+
int[] ids;
62+
63+
public UnionFind(char[][] grid){
64+
m = grid.length;
65+
n = grid[0].length;
66+
ids = new int[m*n];
67+
for(int i = 0; i < m; i++){
68+
for(int j = 0; j < n; j++){
69+
if(grid[i][j] == '1') {
70+
count++;
71+
ids[i*n+j] = i*n+j;
72+
}
73+
}
74+
}
75+
}
76+
77+
public void union(int i, int j){
78+
int x = find(ids, i);
79+
int y = find(ids, j);
80+
if(x != y) {/**note: this is when x != y,
81+
only in this case, we should union these two nodes, which makes sense naturally.*/
82+
count--;
83+
ids[x] = y;//ids[y] = x; //also works
84+
}
85+
}
86+
87+
public int find(int[] ids, int i){
88+
if(ids[i] == i) return i;
89+
return find(ids, ids[i]);
90+
}
91+
}
92+
93+
public int numIslands(char[][] grid) {
94+
if(grid == null || grid.length == 0 || grid[0].length == 0) return 0;
95+
int[] dirs = new int[]{0,1,0,-1,0};
96+
UnionFind uf = new UnionFind(grid);
97+
int m = grid.length, n = grid[0].length;
98+
for(int i = 0; i < m; i++){
99+
for(int j = 0; j < n; j++){
100+
if(grid[i][j] == '1'){
101+
for(int k = 0; k < 4; k++){
102+
int x = i+dirs[k];
103+
int y = j+dirs[k+1];
104+
if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1'){
105+
int id1 = i*n+j;
106+
int id2 = x*n+y;
107+
uf.union(id1, id2);
108+
}
109+
}
110+
}
111+
}
112+
}
113+
return uf.count;
114+
}
115+
}
116+
}

src/main/java/com/fishercoder/solutions/_200DFS.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/main/java/com/fishercoder/solutions/_200UnionFind.java

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._200;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _200Test {
10+
private static _200.DFSSolution dfsSolution;
11+
private static _200.UnionFindSolution unionFindSolution;
12+
private static char[][] grid;
13+
14+
@BeforeClass
15+
public static void setup(){
16+
dfsSolution = new _200.DFSSolution();
17+
unionFindSolution = new _200.UnionFindSolution();
18+
}
19+
20+
@Test
21+
public void test1(){
22+
grid = new char[][]{
23+
{'1','1','1'},
24+
{'0','1','0'},
25+
{'1','1','1'},
26+
};
27+
assertEquals(1, dfsSolution.numIslands(grid));
28+
assertEquals(1, unionFindSolution.numIslands(grid));
29+
}
30+
31+
@Test
32+
public void test2(){
33+
grid = new char[][]{
34+
{'1','1','1','1','0'},
35+
{'1','1','0','1','0'},
36+
{'1','1','0','0','0'},
37+
{'0','0','0','0','0'},
38+
};
39+
assertEquals(0, dfsSolution.numIslands(grid));
40+
assertEquals(0, unionFindSolution.numIslands(grid));
41+
}
42+
43+
@Test
44+
public void test3(){
45+
grid = new char[][]{
46+
{'1','1','0','0','0'},
47+
{'1','1','0','0','0'},
48+
{'0','0','1','0','0'},
49+
{'0','0','0','1','1'},
50+
};
51+
assertEquals(3, dfsSolution.numIslands(grid));
52+
assertEquals(3, unionFindSolution.numIslands(grid));
53+
}
54+
55+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy