为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode537. 复数乘法 | Complex Number Multiplication

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10408940.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given two strings representing two complex numbers.

You need to return a string representing their multiplication. Note i2 = -1 according to the definition.

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:

  1. The input strings will not have extra blank.
  2. The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.

 

给定两个表示复数的字符串。

返回表示它们乘积的字符串。注意,根据定义 i2= -1 。

示例 1:

输入: "1+1i", "1+1i"
输出: "0+2i"
解释: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。

示例 2:

输入: "1+-1i", "1+-1i"
输出: "0+-2i"
解释: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0+-2i 的形式。 

注意:

  1. 输入字符串不包含额外的空格。
  2. 输入字符串将以 a+bi 的形式给出,其中整数 a 和 b 的范围均在 [-100, 100] 之间。输出也应当符合这种形式。

Runtime: 8 ms
Memory Usage: 19.9 MB
 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 }

 

posted @ 2019-02-20 20:11  为敢技术  阅读(393)  评论(0编辑  收藏  举报