Skip to content

Commit 009f159

Browse files
authored
200 solved. (#51)
1 parent dd53604 commit 009f159

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ continually updating 😃.
2323
* [121. Best Time to Buy and Sell Stock](src/0121_best_time_to_buy_and_sell_stock/maxprofit.go)   *`dynamic programming;`*  *`array`*
2424
* [122. Best Time to Buy and Sell Stock II](src/0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)   *`greedy;`*  *`array`*
2525
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
26+
* [200. Number of Islands](src/0200_number_of_island/number_of_island.go)   *`dfs;`*  *`bfs`*
2627
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
2728
* [215. Kth Largest Element in an Array](./src/0215_kth_largest_element_in_an_array/kthleiaa.go)   *`sort`*
2829
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)   *`sliding window`*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
200. Number of Islands
3+
https://leetcode.com/problems/number-of-islands/
4+
5+
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands.
6+
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
7+
You may assume all four edges of the grid are all surrounded by water.
8+
*/
9+
// time: 2019-01-07
10+
11+
package noi
12+
13+
var (
14+
d = [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
15+
m, n int
16+
visited [][]bool
17+
)
18+
19+
// flood-fill
20+
// time complexity: O(n*m)
21+
// space complexity: O(n*m)
22+
func numIslands(grid [][]byte) int {
23+
m = len(grid)
24+
if m <= 0 {
25+
return 0
26+
}
27+
n = len(grid[0])
28+
29+
visited = make([][]bool, m)
30+
for i := 0; i < m; i++ {
31+
visited[i] = make([]bool, n)
32+
}
33+
34+
var res int
35+
36+
for i := 0; i < m; i++ {
37+
for j := 0; j < n; j++ {
38+
if grid[i][j] == '1' && !visited[i][j] {
39+
res++
40+
dfs(grid, i, j)
41+
}
42+
}
43+
}
44+
return res
45+
}
46+
47+
// 从grid[x][y]的位置开始,进行floodfill。
48+
func dfs(grid [][]byte, x, y int) {
49+
visited[x][y] = true
50+
for i := 0; i < len(d); i++ {
51+
newX := x + d[i][0]
52+
newY := y + d[i][1]
53+
if inArea(newX, newY) && !visited[newX][newY] && grid[newX][newY] == '1' {
54+
dfs(grid, newX, newY)
55+
}
56+
}
57+
}
58+
59+
func inArea(x, y int) bool {
60+
return x >= 0 && x < m && y >= 0 && y < n
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package noi
2+
3+
import "testing"
4+
5+
func TestNumIslands(t *testing.T) {
6+
grids := [][][]byte{
7+
{
8+
{'1', '1', '0', '0', '0'},
9+
{'1', '1', '0', '0', '0'},
10+
{'0', '0', '1', '0', '0'},
11+
{'0', '0', '0', '1', '1'},
12+
},
13+
{},
14+
}
15+
expected := []int{3, 0}
16+
17+
for index, grid := range grids {
18+
if res := numIslands(grid); res != expected[index] {
19+
t.Errorf("expected %d, got %d", expected[index], res)
20+
}
21+
}
22+
23+
}

src/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
|0165|[165. Compare Version Numbers](0165_compare_version_numbers/compare_version_numbers.go)|Medium|*`string`*|
6464
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
6565
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
66+
|0200|[200. Number of Islands](0200_number_of_island/number_of_island.go)|Medium|*`dfs;`* *`bfs`*|
6667
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|
6768
|0215|[215. Kth Largest Element in an Array](0215_kth_largest_element_in_an_array/kthleiaa.go)|Medium|*`sort`*|
6869
|0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*|

0 commit comments

Comments
 (0)