File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed
lcof2/剑指 Offer II 118. 多余的边 Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -164,6 +164,36 @@ func findRedundantConnection(edges [][]int) []int {
164
164
}
165
165
```
166
166
167
+ #### Swift
168
+
169
+ ``` swift
170
+ class Solution {
171
+ private var parent: [Int ] = []
172
+
173
+ func findRedundantConnection (_ edges : [[Int ]]) -> [Int ] {
174
+ parent = Array (0 ..< 1010 )
175
+
176
+ for edge in edges {
177
+ let a = edge[0 ]
178
+ let b = edge[1 ]
179
+
180
+ if find (a) == find (b) {
181
+ return edge
182
+ }
183
+ parent[find (a)] = find (b)
184
+ }
185
+ return []
186
+ }
187
+
188
+ private func find (_ x : Int ) -> Int {
189
+ if parent[x] != x {
190
+ parent[x] = find (parent[x])
191
+ }
192
+ return parent[x]
193
+ }
194
+ }
195
+ ```
196
+
167
197
<!-- tabs:end -->
168
198
169
199
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private var parent : [ Int ] = [ ]
3
+
4
+ func findRedundantConnection( _ edges: [ [ Int ] ] ) -> [ Int ] {
5
+ parent = Array ( 0 ..< 1010 )
6
+
7
+ for edge in edges {
8
+ let a = edge [ 0 ]
9
+ let b = edge [ 1 ]
10
+
11
+ if find ( a) == find ( b) {
12
+ return edge
13
+ }
14
+ parent [ find ( a) ] = find ( b)
15
+ }
16
+ return [ ]
17
+ }
18
+
19
+ private func find( _ x: Int ) -> Int {
20
+ if parent [ x] != x {
21
+ parent [ x] = find ( parent [ x] )
22
+ }
23
+ return parent [ x]
24
+ }
25
+ }
You can’t perform that action at this time.
0 commit comments