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

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 }

转载于:https://www.cnblogs.com/strengthen/p/10408940.html

[Swift]LeetCode537. 复数乘法 | Complex Number Multiplication相关推荐

  1. 【leetcode】537. Complex Number Multiplication(Python C++)

    537. Complex Number Multiplication 题目链接 537.1 题目描述: Given two strings representing two complex numbe ...

  2. leetcode【537】Complex Number Multiplication(复数相乘)

    写在最前面:一道很常规的字符串分割的题 leetcode[537]Complex Number Multiplication Given two strings representing two co ...

  3. LeetCode 537. Complex Number Multiplication

    Given two strings representing two complex numbers. You need to return a string representing their m ...

  4. Leetcode——537. Complex Number Multiplication

    题目原址 https://leetcode.com/problems/complex-number-multiplication/description/ 题目描述 Given two strings ...

  5. 复数 Complex Number 教程

    复数 Complex Number 教程

  6. C++实现complex number复数的算法(附完整源码)

    C++实现complex number复数的算法 C++实现complex number复数的算法完整源码(定义,实现,main函数测试) C++实现complex number复数的算法完整源码(定 ...

  7. SWIFT Code 和 Routing Number 的关系

    SWIFT Code 该号相当于各个银行的身份证号 每个银行(包括每个分行.支行)都有一个代码,是由银行名称的英文缩写和总行所在地的英文缩写(也有用数字加字母表示某城市的)以及该分行所在地的代码(字母 ...

  8. leetcode537. 复数乘法

    给定两个表示复数的字符串. 返回表示它们乘积的字符串.注意,根据定义 i2 = -1 . 示例 1: 输入: "1+1i", "1+1i" 输出: " ...

  9. 1748. The Most Complex Number/LG的数学计划~~~持续更新ing(反素数求解)

    神奇的反素数, 首先定义 g(x) = x的约数个数 而反素数就是对于任意的0 < j < i 有g(j) < g(i)那么就称i为反素数 那么从这个定义中可以发现的是, 反素数一定 ...

最新文章

  1. 基于 Webpack 3 的 React 工程项目脚手架
  2. 推荐一款日志切割神器
  3. JS拼凑方法之join
  4. j - 数据结构实验:哈希表_一看就懂的数据结构基础「哈希表」
  5. 洛谷 P4430 小猴打架
  6. 任务二 用户注册界面设计
  7. 盘点关于Java在生活中的应用!
  8. Linux进行设置环境变量
  9. PHP常用的正则表达式(有些需要调整)
  10. Openstack(二)基本环境准备--网络、时间、yum源等
  11. 列级触发器 SQL Server
  12. 1.vue生命周期详解(2020.12.05)
  13. Ubuntu 安装字体方法
  14. 进价移动加权核算体系
  15. 内存和硬盘在计算机中的作用,电脑内存的作用 内存条的作用到底是什么
  16. pyqt5事件与鼠标事件
  17. 武汉体育学院计算机设计大赛,2017年(第10届)中国大学生计算机设计大赛中南地区赛作品评审结果公告.PDF...
  18. tomcat介绍:安装与优化
  19. Traffic Light
  20. 选择城市,按城市的首字母进行排序

热门文章

  1. 第一个Java程序:HelloWorld!
  2. Linux/Unix 效率工具:快速路径切换 z 命令
  3. Floccus – 跨平台浏览器「书签同步」插件
  4. android百度地图拖拽地图定位,百度地图的定位以及拖拽(显示坐标位置)
  5. 项目脚手架VueCLI23
  6. 第3章 信息技术服务知识
  7. 驾考笔记:科目三满分攻略——荣县郝家坝科目三细节及线路详解
  8. 使用java.lang.reflect.Method.invoke抛出java.lang.IllegalArgumentException: argument type mismatch异常
  9. 张景明:方剂【方歌】——解表剂
  10. 中策橡胶采用ET工业大脑效果显著,获选IDC中国数字化转型大奖