【LeetCode】537. Complex Number Multiplication 解题报告(Python & C++)

时间:2021-12-30 06:01:32

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/complex-number-multiplication/description/

题目描述

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.

解题方法

方法一:

求两个复数的乘积。这个题中已经说明了一定会存在+号,这就是留个我们根据格式化的表达读取数字的实虚部用的。

另外,注意字符串的格式化时,后面所有的变量必须用括号括起来才行,也就是tuple型。

class Solution(object):
def complexNumberMultiply(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
num1 = map(int, a[:-1].split('+'))
num2 = map(int, b[:-1].split('+'))
real = num1[0] * num2[0] - num1[1] * num2[1]
virtual = num1[0] * num2[1] + num1[1] * num2[0]
return "%d+%di" % (real, virtual)

C++处理的字符串的时候稍微麻烦了一些。

class Solution {
public:
string complexNumberMultiply(string a, string b) {
int ap = a.find('+');
int ar = stoi(a.substr(0, ap));
int ai = stoi(a.substr(ap + 1, a.size() - 1));
int bp = b.find('+');
int br = stoi(b.substr(0, bp));
int bi = stoi(b.substr(bp + 1, b.size() - 1));
string ansr = to_string(ar * br - ai * bi);
string ansi = to_string(ar * bi + ai * br);
return ansr + '+' + ansi + 'i';
}
};

日期

2018 年 2 月 5 日
2018 年 12 月 4 日 —— 周二啦!

【LeetCode】537. Complex Number Multiplication 解题报告(Python & C++)的更多相关文章

  1. LC 537. Complex Number Multiplication

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

  2. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  3. LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor

    1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...

  4. 537 Complex Number Multiplication 复数乘法

    详见:https://leetcode.com/problems/complex-number-multiplication/description/ C++: class Solution { pu ...

  5. 537. Complex Number Multiplication

    题目大意: 给出a, b两个用字符串表示的虚数,求a*b 题目思路: 偷了个懒,Python3的正则表达式匹配了一下,当然acm里肯定是不行的 class Solution: def complexN ...

  6. LeetCode 537. 复数乘法(Complex Number Multiplication)

    537. 复数乘法 537. Complex Number Multiplication 题目描述 Given two strings representing two complex numbers ...

  7. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  8. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  9. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. Jqgrid入门-Jqgrid分组的实现(八)

    上一章主要说明了如果实现Jqgrid列数据拖动,这一章主要讨论在Jqgrid中如何实现分组功能.         类似于Sql语句的Group By,Jqgrid提供了属性实现数据分组,这样表现数据会 ...

  2. 数据结构典型算法的VC实现(袁辉勇)

    1. 迷宫问题求解 #include <stdio.h> #define m 8 //迷宫内有8列 #define n 8 //迷宫内有8行 #define MAXSIZE 100//栈尺 ...

  3. Protobuf的自动反射消息类型的方法

    1. 每个消息头部中带上type name,作为消息的类型标识 2. 通过type name可以找到描述符Descriptor*, FindMessageTypeByName 3. 通过描述符Desc ...

  4. oracle11g 体系结构详解

    1.oracle内存由SGA+PGA所构成 2.oracle数据库体系结构数据库的体系结构是指数据库的组成.工作过程与原理,以及数据在数据库中的组织与管理机制. oracle工作原理: 1).在数据库 ...

  5. python默认编码设置

      打开python 的gui,输入 1 2 import sys sys.getdefaultencoding() 查询系统当前默认编码 默认情况下显示编码方式为ASCII 在python安装目录下 ...

  6. 设置dataGridView单元格颜色、字体、ToolTip、字体颜色

    this.dataGridView3.Rows[e.RowIndex].Cells["你的那个要判断的列名"].Style.BackColor = Color.MediumPurp ...

  7. antd-design model 数据特点

  8. CSS选择器、层叠相关的基础知识

    CSS是Cascading Style Sheets的英文缩写,即层叠样式表.CSS2.1是W3C于2007年发布,现在推荐使用的.CSS3现在还处于开发中,有部分浏览器的新版本支持. 1. CSS ...

  9. 数据库链接池 durid 的配置参数详解

    这里我主要介绍druid 比较重要的参数解释,不做druid的使用介绍,druid虽然功能强大,但是如果对配置参数理解不到位,性能非但不能达到很优,而且会出现很多异常,所以使用druid之前一定要清楚 ...

  10. 封装locaostorage

    const ls = localStorage export default { setItem(name, value) { ls.setItem(name, JSON.stringify(valu ...