【LeetCode】537. Complex Number Multiplication 解题报告(Python & C++)
作者: 负雪明烛
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:
- The input strings will not have extra blank.
- 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++)的更多相关文章
- LC 537. Complex Number Multiplication
Given two strings representing two complex numbers. You need to return a string representing their m ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor
1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...
- 537 Complex Number Multiplication 复数乘法
详见:https://leetcode.com/problems/complex-number-multiplication/description/ C++: class Solution { pu ...
- 537. Complex Number Multiplication
题目大意: 给出a, b两个用字符串表示的虚数,求a*b 题目思路: 偷了个懒,Python3的正则表达式匹配了一下,当然acm里肯定是不行的 class Solution: def complexN ...
- LeetCode 537. 复数乘法(Complex Number Multiplication)
537. 复数乘法 537. Complex Number Multiplication 题目描述 Given two strings representing two complex numbers ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
随机推荐
- 搭建简单的SpringCloud项目三:问题及解决
GitHub:https://github.com/ownzyuan/test-cloud 前篇:搭建简单的SpringCloud项目一:注册中心和公共层 搭建简单的SpringCloud项目二:服务 ...
- 工作学习1-tcp自连接
运维同事反馈服务起不来.下面为了方便,写了一个demo来展示. https://gitee.com/northeast_coder/code/tree/master/case/case1_tcp_se ...
- A Child's History of England.5
Above all, it was in the Roman time, and by means of Roman ships, that the Christian Religion was fi ...
- R语言学习记录(二)
4.对象改值 4.1.就地改值 比如: vec <- c(0,0,0,0,0,0,0) vec[1]<-100 #vec向量的第一个值就变为100 ####对于数据框的改值的方法,如下面的 ...
- LeetCode382-链表随机节点
原题链接:[382. 链表随机节点]:https://leetcode-cn.com/problems/linked-list-random-node/ 题目描述: 给定一个单链表,随机选择链表的一个 ...
- 【leetocode】55. Jump Game
You are given an integer array nums. You are initially positioned at the array's first index, and ea ...
- 如何在Swagger2或Swagger3中增加Json Web Token
1. 前言 Swagger 3.0已经发布有一段时间了,作为一个非常有用的文档工具已经越来越多的项目在使用它.而JWT也是目前前后端分离最常用的安全技术.那么如何在Swagger 3.0 中添加JWT ...
- html5 绘图
SVG 在 SVG 中,每个元素是图型与数据相结合的一个对象. 修改对象属性的值,图型会立即体现出这种变化. 因为是对象,所以支持事件处理. D3使用的是SVG Canvas 不支持事件处理. cha ...
- clickhouse 输入输出格式
TabSeparated.TabSeparatedRaw.TabSeparatedWithNames和TabSeparatedWithNamesAndTypes TabSeparated 默认格式,缩 ...
- Python初探——sklearn库中数据预处理函数fit_transform()和transform()的区别
敲<Python机器学习及实践>上的code的时候,对于数据预处理中涉及到的fit_transform()函数和transform()函数之间的区别很模糊,查阅了很多资料,这里整理一下: ...