mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 947
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package leetcode
|
||||
|
||||
func removeStones(stones [][]int) int {
|
||||
if len(stones) <= 1 {
|
||||
return 0
|
||||
}
|
||||
uf, rowMap, colMap := UnionFind{}, map[int]int{}, map[int]int{}
|
||||
uf.init(len(stones))
|
||||
for i := 0; i < len(stones); i++ {
|
||||
if _, ok := rowMap[stones[i][0]]; ok {
|
||||
uf.union(rowMap[stones[i][0]], i)
|
||||
} else {
|
||||
rowMap[stones[i][0]] = i
|
||||
}
|
||||
if _, ok := colMap[stones[i][1]]; ok {
|
||||
uf.union(colMap[stones[i][1]], i)
|
||||
} else {
|
||||
colMap[stones[i][1]] = i
|
||||
}
|
||||
}
|
||||
return len(stones) - uf.totalCount()
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question947 struct {
|
||||
para947
|
||||
ans947
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para947 struct {
|
||||
stones [][]int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans947 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem947(t *testing.T) {
|
||||
|
||||
qs := []question947{
|
||||
question947{
|
||||
para947{[][]int{[]int{0, 0}, []int{0, 1}, []int{1, 0}, []int{1, 2}, []int{2, 1}, []int{2, 2}}},
|
||||
ans947{5},
|
||||
},
|
||||
|
||||
question947{
|
||||
para947{[][]int{[]int{0, 0}, []int{0, 2}, []int{1, 1}, []int{2, 0}, []int{2, 2}}},
|
||||
ans947{3},
|
||||
},
|
||||
|
||||
question947{
|
||||
para947{[][]int{[]int{0, 0}}},
|
||||
ans947{0},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 947------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans947, q.para947
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, removeStones(p.stones))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
Reference in New Issue
Block a user