-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200.go
60 lines (57 loc) · 1.22 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
package main
import (
"fmt"
"strconv"
)
func numIslands(grid [][]byte) int {
if len(grid) == 0 {
return 0
}
m, n := len(grid), len(grid[0])
marked := make(map[string]bool)
num := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
key := strconv.Itoa(i) + strconv.Itoa(j)
if _, ok := marked[key]; !ok{
if grid[i][j] == []byte("0")[0] {
marked[key] = true
continue
}
num += 1
labers := [][]int{{i, j}}
for len(labers) != 0 {
r, c := labers[0][0], labers[0][1]
labers = labers[1:]
if 0 <= r && r < m && 0 <= c && c < n {
_key := strconv.Itoa(r) + strconv.Itoa(c)
if _, ok := marked[_key]; !ok{
marked[_key] = true
if grid[r][c] == []byte("1")[0] {
labers = append(labers, [][]int{{r, c - 1}, {r, c + 1}, {r - 1, c}, {r + 1, c}}...)
}
}
}
}
}
}
}
return num
}
func main() {
param := [][]byte{
{'1', '1', '1', '1', '0'},
{'1', '1', '0', '1', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '0', '0', '0'},
}
a := numIslands(param)
fmt.Print(a)
param2 := [][]byte{
{'1', '1', '0', '0', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '1', '0', '0'},
{'0', '0', '0', '1', '1'},
}
fmt.Print(numIslands(param2))
}