作者: 负雪明烛
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. Shell中 ##%% 操作变量名

    在linxu平台下少不了对变量名的处理,今天记录下shell中 ##%% 对变量名的操作. #操作左侧,%操作右侧. #号处理方式: 对于单个#,处理对象为变量中指定的第一个符号左侧字符串, 对于两个 ...

  2. 60-Lowest Common Ancestor of a Binary Search Tree

    Lowest Common Ancestor of a Binary Search Tree My Submissions QuestionEditorial Solution Total Accep ...

  3. centos安装后的个人工具

    1.安装vim工具 yum -y install vim 安装完成后在家目录下新建一个.vimrc的配置文件.辅助vim软件功能. set number " 显示行号 set cursorl ...

  4. 分布式服务治理框架Dubbo的前世今生及应用实战

    Dubbo的出现背景 Dubbo从开源到现在,已经出现了接近10年时间,在国内各大企业被广泛应用. 它到底有什么魔力值得大家去追捧呢?本篇文章给大家做一个详细的说明. 大规模服务化对于服务治理的要求 ...

  5. a这个词根

    a是个词根,有三种意思:1. 以某种状态或方式,如: ablaze, afire, aflame, alight, aloud, alive, afloat等2. at, in, on, to sth ...

  6. Hive(二)【数据类型、类型转换】

    目录 一.基本数据类型 案例实操 二.集合数据类型 案例实操 Map类型 三.类型转换 1.隐式类型转换 2.显示(强制)类型转换 一.基本数据类型 HIVE MySQL JAVA 长度 例子 TIN ...

  7. android:为TextView添加样式、跑马灯、TextSwitcher和ImageSwitcher实现平滑过渡

    一.样式 设置下划线: textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线 textView.getPaint().setAnt ...

  8. spring 事务处理中,同一个类中:A方法(无事务)调B方法(有事务),事务不生效问题

    public class MyEntry implements IBaseService{ public String A(String jsonStr) throws Exception{ User ...

  9. Java Spring 自定义事件监听

    ApplicationContext 事件 定义一个context的起动监听事件 import org.springframework.context.ApplicationListener; imp ...

  10. When do we use Initializer List in C++?

    Initializer List is used to initialize data members of a class. The list of members to be initialize ...