★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-krpscosl-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given two strings representing two complex numbers.git
You need to return a string representing their multiplication. Note i2 = -1 according to the definition.github
Example 1:數組
Input: "1+1i", "1+1i" Output: "0+2i" Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
Example 2:微信
Input: "1+-1i", "1+-1i" Output: "0+-2i" Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
Note:this
給定兩個表示複數的字符串。spa
返回表示它們乘積的字符串。注意,根據定義 i2= -1 。code
示例 1:component
輸入: "1+1i", "1+1i" 輸出: "0+2i" 解釋: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你須要將它轉換爲 0+2i 的形式。
示例 2:orm
輸入: "1+-1i", "1+-1i" 輸出: "0+-2i" 解釋: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你須要將它轉換爲 0+-2i 的形式。
注意:
1 class Solution { 2 func complexNumberMultiply(_ a: String, _ b: String) -> String { 3 var a = a 4 var b = b 5 //刪除最後一個i 6 a.remove(at:a.index(before:a.endIndex)) 7 b.remove(at:b.index(before:b.endIndex)) 8 //字符串轉數組 9 let arrA:[String] = a.components(separatedBy:"+") 10 let arrB:[String] = b.components(separatedBy:"+") 11 let c1:Complex = Complex(real: Int(arrA[0])!, img: Int(arrA[1])!) 12 let c2:Complex = Complex(real: Int(arrB[0])!, img: Int(arrB[1])!) 13 return (c1 * c2).toString 14 } 15 } 16 17 /*複數結構*/ 18 struct Complex { 19 //實部 20 var real: Int 21 //虛部 22 var img: Int 23 24 //將複數轉換成字符串。 25 var toString: String { 26 return "\(real)+\(img)i" 27 } 28 29 //重載乘法運算符 30 //設z1=a+bi,z2=c+di(a、b、c、d∈R)是任意兩個複數,那麼它們的積(a+bi)(c+di)=(ac-bd)+(bc+ad)i 31 static func *(_ x: Complex,_ y: Complex) -> Complex { 32 return Complex(real: (x.real * y.real - x.img * y.img), img: (x.img * y.real + x.real * y.img)) 33 } 34 }
8ms
1 final class Solution { 2 func complexNumberMultiply(_ a: String, _ b: String) -> String { 3 let a = Array(a) 4 let b = Array(b) 5 let (realA, imagA) = splitComplex(a) 6 let (realB, imagB) = splitComplex(b) 7 let realC = realA &* realB &- imagA &* imagB 8 let imagC = realA &* imagB &+ realB &* imagA 9 return "\(realC)+\(imagC)i" 10 } 11 12 @inline(__always) private func splitComplex(_ a: [Character]) -> (Int, Int) { 13 var i = 0 14 var j = 0 15 var realA = 0 16 var imagA = 0 17 while i < a.count { 18 if a[i] == "+" { 19 realA = Int(String(a[0..<i]))! 20 j = i &+ 1 21 } else if a[i] == "i" { 22 imagA = Int(String(a[j..<i]))! 23 } 24 i &+= 1 25 } 26 return (realA, imagA) 27 } 28 }
12ms
1 class Solution { 2 func complexNumberMultiply(_ a: String, _ b: String) -> String { 3 let firstParts = a.split(separator: "+") 4 let secondParts = b.split(separator: "+") 5 6 guard firstParts.count == 2 && secondParts.count == 2 else { 7 return "" 8 } 9 10 // for given a = a+bi, and b = c+di 11 // multiplication of a and b can be summurized into ac-bd+(ad+bc)i as i^2 will be -1 as pre-defined 12 guard let a = Int(firstParts[0]), let b = Int(firstParts[1].replacingOccurrences(of: "i", with: "")), let c = Int(secondParts[0]), let d = Int(secondParts[1].replacingOccurrences(of: "i", with: "")) else { 13 return "" 14 } 15 16 return "\((a*c)-(b*d))+\((a*d)+(b*c))i" 17 } 18 }
16ms
1 class Solution { 2 func complexNumberMultiply(_ a: String, _ b: String) -> String { 3 let strArray1 = a.split(separator: "+") 4 let strArray2 = b.split(separator: "+") 5 var resultString = "" 6 var constantTerm = 0 7 var ithTerm = 0 8 let product = -1 9 10 for i in 0..<strArray1.count { 11 for j in 0..<strArray2.count { 12 if i == 0 && j == 0 { 13 guard let a = Int(strArray1[i]), let b = Int(strArray2[j]) else { return String() } 14 constantTerm = a*b 15 } else if i == strArray1.count-1 && j == strArray2.count-1 { 16 let str1 = strArray1[i] 17 let str2 = strArray2[j] 18 let endIndex1 = str1.index(str1.startIndex, offsetBy: str1.count-1) 19 let endIndex2 = str2.index(str2.startIndex, offsetBy: str2.count-1) 20 guard let constantStr1 = Int(str1[str1.startIndex..<endIndex1]), 21 let constantStr2 = Int(str2[str2.startIndex..<endIndex2]) else { return String() } 22 let tempConstant = constantStr1 * constantStr2 23 constantTerm -= tempConstant 24 } else { 25 let str1 = strArray1[i] 26 let str2 = strArray2[j] 27 var constant1 = 0 28 var constant2 = 0 29 30 if let constantStr1 = Int(str1[str1.startIndex..<str1.endIndex]) { 31 constant1 = constantStr1 32 } else { 33 let endIndex1 = str1.index(str1.startIndex, offsetBy: str1.count-1) 34 guard let constantStr1 = Int(str1[str1.startIndex..<endIndex1]) else { return String() } 35 constant1 = constantStr1 36 } 37 38 if let constantStr2 = Int(str2[str2.startIndex..<str2.endIndex]) { 39 constant2 = constantStr2 40 } else { 41 let endIndex1 = str2.index(str2.startIndex, offsetBy: str2.count-1) 42 guard let constantStr2 = Int(str2[str2.startIndex..<endIndex1]) else { return String() } 43 constant2 = constantStr2 44 } 45 let tempConstant = constant1*constant2 46 ithTerm += tempConstant 47 } 48 } 49 } 50 return String(constantTerm)+"+"+String(ithTerm)+"i" 51 } 52 }
20ms
1 class Solution { 2 func complexNumberMultiply(_ a: String, _ b: String) -> String { 3 let avals = a.components(separatedBy: "+") 4 let bvals = b.components(separatedBy: "+") 5 6 let r = Int(avals[0])! 7 let t = Int(avals[1].replacingOccurrences(of: "i", with: ""))! 8 9 let s = Int(bvals[0])! 10 let d = Int(bvals[1].replacingOccurrences(of: "i", with: ""))! 11 12 let v1 = r * s - t * d 13 let v2 = r * d + t * s 14 15 return "\(v1)+\(v2)i" 16 } 17 }