number-of-islands
题目链接:https://leetcode.com/problems/number-of-islands/
这是一道很经典的题,shopee也有考过。
最常见的解法是DFS:
func numIslands(grid [][]byte) int {
row := len(grid)
if row == 0 {
return 0
}
col := len(grid[0])
var cnt int
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
if grid[i][j] == '1' {
dfsMark(grid, row, col, i, j)
cnt++
}
}
}
return cnt
}
func dfsMark(grid [][]byte, row int, col int, i int, j int) {
if i < 0 || i >= row || j < 0 || j >= col || grid[i][j] != '1' {
return
}
grid[i][j] = '0'
dfsMark(grid, row, col, i-1, j)
dfsMark(grid, row, col, i+1, j)
dfsMark(grid, row, col, i, j-1)
dfsMark(grid, row, col, i, j+1)
}
本文要探讨的是它的第二种解法,基于并查集的。
func numIslands(grid [][]byte) int {
row := len(grid)
if row == 0 {
return 0
}
col := len(grid[0])
uf := NewUnionFind(row * col)
directions := [][]int{{1, 0}, {0, 1}}
var zeroCnt int
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
if grid[i][j] == '1' {
for _, v := range directions {
x := i + v[0]
y := j + v[1]
if x >= 0 && x < row && y >= 0 && y < col && grid[x][y] == '1' {
uf.Union(i*col+j, x*col+y)
}
}
} else {
zeroCnt++
}
}
}
return uf.Cnt() - zeroCnt
}
type UnionFind struct {
parent []int
rank []int
cnt int
}
func NewUnionFind(n int) *UnionFind {
uf := &UnionFind{
cnt: n,
parent: make([]int, n),
rank: make([]int, n),
}
for i := 0; i < n; i++ {
uf.parent[i] = i
}
return uf
}
func (uf *UnionFind) Find(x int) int {
if uf.parent[x] == x {
return x
}
uf.parent[x] = uf.Find(uf.parent[x])
return uf.parent[x]
}
func (uf *UnionFind) Union(i int, j int) {
pi := uf.Find(i)
pj := uf.Find(j)
if pi == pj {
return
}
if uf.rank[pi] > uf.rank[pj] {
uf.parent[pj] = pi
} else {
uf.parent[pi] = pj
if uf.rank[pi] == uf.rank[pj] {
uf.rank[pj]++
}
}
uf.cnt--
}
func (uf *UnionFind) Cnt() int {
return uf.cnt
}
并查集的方法一旦掌握,就可以用于解决很多类似的问题。
number-of-islands 2
题目链接:https://www.lintcode.com/problem/434
type Point struct {
X int
Y int
}
func NumIslands2(n int, m int, operators []*Point) []int {
res := make([]int, len(operators))
uf := NewUnionFind(n * m)
directions := [][]int{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}
for i := 0; i < len(operators); i++ {
x := operators[i].X
y := operators[i].Y
if uf.GetParent(x*m+y) != -1 {
res[i] = uf.GetCnt()
continue
}
uf.SetParent(x*m + y)
for _, v := range directions {
xx := x + v[0]
yy := y + v[1]
if xx >= 0 && xx < n && yy >= 0 && yy < m && uf.GetParent(xx*m+yy) != -1 {
uf.Union(x*m+y, xx*m+yy)
}
}
res[i] = uf.GetCnt()
}
return res
}
type UnionFind struct {
parent []int
rank []int
cnt int
}
func NewUnionFind(n int) *UnionFind {
uf := &UnionFind{
parent: make([]int, n),
rank: make([]int, n),
}
for i := 0; i < n; i++ {
uf.parent[i] = -1
}
return uf
}
func (uf *UnionFind) Find(x int) int {
if uf.parent[x] == x {
return x
}
uf.parent[x] = uf.Find(uf.parent[x])
return uf.parent[x]
}
func (uf *UnionFind) Union(i int, j int) {
pi := uf.Find(i)
pj := uf.Find(j)
if pi == pj {
return
}
if uf.rank[pi] > uf.rank[pj] {
uf.parent[pj] = pi
} else {
uf.parent[pi] = pj
if uf.rank[pi] == uf.rank[pj] {
uf.rank[pj]++
}
}
uf.cnt--
}
func (uf *UnionFind) SetParent(id int) {
uf.parent[id] = id
uf.cnt++
}
func (uf *UnionFind) GetParent(id int) int {
return uf.parent[id]
}
func (uf *UnionFind) GetCnt() int {
return uf.cnt
}
参考资料
https://leetcode.com/problems/number-of-islands/discuss/56359/Very-concise-Java-AC-solution
https://leetcode.com/problems/number-of-islands/discuss/56354/1D-Union-Find-Java-solution-easily-generalized-to-other-problems
https://www.cnblogs.com/grandyang/p/5190419.html
https://aaronice.gitbook.io/lintcode/union_find/number_of_islands_ii