-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200.岛屿数量.go
77 lines (70 loc) · 1.35 KB
/
200.岛屿数量.go
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
69
70
71
72
73
74
75
76
77
/*
* @lc app=leetcode.cn id=200 lang=golang
*
* [200] 岛屿数量
*/
// @lc code=start
var Directions [][]int = [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}
/**
* DFS
*/
func numIslands_1(grid [][]byte) int {
ret := 0
row, col := len(grid), len(grid[0])
var dfs func(row, col int)
dfs = func(x, y int) {
if x >= row || y >= col || x < 0 || y < 0 || grid[x][y] == '0' {
return
}
grid[x][y] = '0'
for _, direction := range Directions {
x1, y1 := x+direction[0], y+direction[1]
dfs(x1, y1)
}
}
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
if grid[i][j] == '1' {
ret++
dfs(i, j)
}
}
}
return ret
}
/**
* BFS
*/
func numIslands(grid [][]byte) int {
ret := 0
row, col := len(grid), len(grid[0])
var bfs func(row, col int)
bfs = func(x, y int) {
q := [][]int{{x, y}}
for len(q) > 0 {
size := len(q)
for i := 0; i < size; i++ {
posX, posY := q[i][0], q[i][1]
if posX >= row || posY >= col || posX < 0 || posY < 0 || grid[posX][posY] == '0' {
continue
}
grid[posX][posY] = '0'
for _, direction := range Directions {
x1, y1 := posX+direction[0], posY+direction[1]
q = append(q, []int{x1, y1})
}
}
q = q[size:]
}
}
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
if grid[i][j] == '1' {
ret++
bfs(i, j)
}
}
}
return ret
}
// @lc code=end