We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 075f00d commit 3ca52c4Copy full SHA for 3ca52c4
537_ComplexNumberMultiplication.swift
@@ -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