mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 684
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package leetcode
|
||||
|
||||
func findRedundantConnection(edges [][]int) []int {
|
||||
if len(edges) == 0 {
|
||||
return []int{}
|
||||
}
|
||||
uf, res := UnionFind{}, []int{}
|
||||
uf.init(len(edges) + 1)
|
||||
for i := 0; i < len(edges); i++ {
|
||||
if uf.find(edges[i][0]) != uf.find(edges[i][1]) {
|
||||
uf.union(edges[i][0], edges[i][1])
|
||||
} else {
|
||||
res = append(res, edges[i][0])
|
||||
res = append(res, edges[i][1])
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question684 struct {
|
||||
para684
|
||||
ans684
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para684 struct {
|
||||
one [][]int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans684 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem684(t *testing.T) {
|
||||
|
||||
qs := []question684{
|
||||
|
||||
question684{
|
||||
para684{[][]int{[]int{1, 2}, []int{1, 3}, []int{2, 3}}},
|
||||
ans684{[]int{2, 3}},
|
||||
},
|
||||
|
||||
question684{
|
||||
para684{[][]int{[]int{1, 2}, []int{2, 3}, []int{3, 4}, []int{1, 4}, []int{1, 5}}},
|
||||
ans684{[]int{1, 4}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 684------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans684, q.para684
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, findRedundantConnection(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
Reference in New Issue
Block a user