Skip to content

Commit 3ca52c4

Browse files
committed
Add solution 537
1 parent 075f00d commit 3ca52c4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

537_ComplexNumberMultiplication.swift

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
func complexNumberMultiply(_ a: String, _ b: String) -> String {
3+
let (a1, b1) = splitComplexNumber(a)
4+
let (a2, b2) = splitComplexNumber(b)
5+
let A = a1*a2 - b1*b2
6+
let B = a1*b2 + a2*b1
7+
return "\(A)+\(B)i"
8+
}
9+
10+
func splitComplexNumber(_ n: String) -> (Int, Int) {
11+
let components = n.split(separator: "+")
12+
let a = components[0]
13+
let b = components[1]
14+
return (Int(a)!, Int(b.prefix(b.count-1))!)
15+
}
16+
}

0 commit comments

Comments
 (0)