File tree Expand file tree Collapse file tree 2 files changed +127
-0
lines changed
lcof2/剑指 Offer II 111. 计算除法 Expand file tree Collapse file tree 2 files changed +127
-0
lines changed Original file line number Diff line number Diff line change @@ -292,6 +292,72 @@ func find(x int) int {
292292}
293293```
294294
295+ #### Swift
296+
297+ ``` swift
298+ class Solution {
299+ private var parent = [Int ]()
300+ private var weight = [Double ]()
301+
302+ func calcEquation (
303+ _ equations : [[String ]],
304+ _ values : [Double ],
305+ _ queries : [[String ]]
306+ ) -> [Double ] {
307+ let n = equations.count
308+ parent = Array (0 ..< (n * 2 ))
309+ weight = Array (repeating : 1.0 , count : n * 2 )
310+
311+ var map = [String : Int ]()
312+ var index = 0
313+
314+ for i in 0 ..< n {
315+ let a = equations[i][0 ]
316+ let b = equations[i][1 ]
317+
318+ if map[a] == nil {
319+ map[a] = index
320+ index += 1
321+ }
322+ if map[b] == nil {
323+ map[b] = index
324+ index += 1
325+ }
326+
327+ let pa = find (map[a]! )
328+ let pb = find (map[b]! )
329+
330+ if pa != pb {
331+ parent[pa] = pb
332+ weight[pa] = weight[map[b]! ] * values[i] / weight[map[a]! ]
333+ }
334+ }
335+
336+ var result = [Double ]()
337+
338+ for query in queries {
339+ let (c, d) = (query[0 ], query[1 ])
340+ if let id1 = map[c], let id2 = map[d], find (id1) == find (id2) {
341+ result.append (weight[id1] / weight[id2])
342+ } else {
343+ result.append (-1.0 )
344+ }
345+ }
346+
347+ return result
348+ }
349+
350+ private func find (_ x : Int ) -> Int {
351+ if parent[x] != x {
352+ let origin = parent[x]
353+ parent[x] = find (parent[x])
354+ weight[x] *= weight[origin]
355+ }
356+ return parent[x]
357+ }
358+ }
359+ ```
360+
295361<!-- tabs:end -->
296362
297363<!-- solution:end -->
Original file line number Diff line number Diff line change 1+ class Solution {
2+ private var parent = [ Int] ( )
3+ private var weight = [ Double] ( )
4+
5+ func calcEquation(
6+ _ equations: [ [ String ] ] ,
7+ _ values: [ Double ] ,
8+ _ queries: [ [ String ] ]
9+ ) -> [ Double ] {
10+ let n = equations. count
11+ parent = Array ( 0 ..< ( n * 2 ) )
12+ weight = Array ( repeating: 1.0 , count: n * 2 )
13+
14+ var map = [ String: Int] ( )
15+ var index = 0
16+
17+ for i in 0 ..< n {
18+ let a = equations [ i] [ 0 ]
19+ let b = equations [ i] [ 1 ]
20+
21+ if map [ a] == nil {
22+ map [ a] = index
23+ index += 1
24+ }
25+ if map [ b] == nil {
26+ map [ b] = index
27+ index += 1
28+ }
29+
30+ let pa = find ( map [ a] !)
31+ let pb = find ( map [ b] !)
32+
33+ if pa != pb {
34+ parent [ pa] = pb
35+ weight [ pa] = weight [ map [ b] !] * values[ i] / weight[ map [ a] !]
36+ }
37+ }
38+
39+ var result = [ Double] ( )
40+
41+ for query in queries {
42+ let ( c, d) = ( query [ 0 ] , query [ 1 ] )
43+ if let id1 = map [ c] , let id2 = map [ d] , find ( id1) == find ( id2) {
44+ result. append ( weight [ id1] / weight[ id2] )
45+ } else {
46+ result. append ( - 1.0 )
47+ }
48+ }
49+
50+ return result
51+ }
52+
53+ private func find( _ x: Int ) -> Int {
54+ if parent [ x] != x {
55+ let origin = parent [ x]
56+ parent [ x] = find ( parent [ x] )
57+ weight [ x] *= weight [ origin]
58+ }
59+ return parent [ x]
60+ }
61+ }
You can’t perform that action at this time.
0 commit comments