From d8801201ce08a83aad9ec99fea572b5afed7bb17 Mon Sep 17 00:00:00 2001 From: YDZ Date: Mon, 29 Jul 2019 22:17:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20684?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../684. Redundant Connection.go | 18 +++++++ .../684. Redundant Connection_test.go | 47 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 Algorithms/0684. Redundant Connection/684. Redundant Connection.go create mode 100644 Algorithms/0684. Redundant Connection/684. Redundant Connection_test.go diff --git a/Algorithms/0684. Redundant Connection/684. Redundant Connection.go b/Algorithms/0684. Redundant Connection/684. Redundant Connection.go new file mode 100644 index 00000000..71ccfc4d --- /dev/null +++ b/Algorithms/0684. Redundant Connection/684. Redundant Connection.go @@ -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 +} diff --git a/Algorithms/0684. Redundant Connection/684. Redundant Connection_test.go b/Algorithms/0684. Redundant Connection/684. Redundant Connection_test.go new file mode 100644 index 00000000..2e30735d --- /dev/null +++ b/Algorithms/0684. Redundant Connection/684. Redundant Connection_test.go @@ -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") +}