[LeetCode] Complex Number Multiplication 复数相乘
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 + i
2
+ 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 + i
2
- 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.
这道题让我们求复数的乘法,有关复数的知识最早还是在本科的复变函数中接触到的,难起来还真是难。但是这里只是最简单的乘法,只要利用好定义i2=-1就可以解题,而且这道题的另一个考察点其实是对字符的处理,我们需要把字符串中的实部和虚部分离开并进行运算,那么我们可以用STL中自带的find_last_of函数来找到加号的位置,然后分别拆出实部虚部,进行运算后再变回字符串,参见代码如下:
解法一:
class Solution {
public:
string complexNumberMultiply(string a, string b) {
int n1 = a.size(), n2 = b.size();
auto p1 = a.find_last_of("+"), p2 = b.find_last_of("+");
int a1 = stoi(a.substr(, p1)), b1 = stoi(b.substr(, p2));
int a2 = stoi(a.substr(p1 + , n1 - p1 - ));
int b2 = stoi(b.substr(p2 + , n2 - p2 - ));
int r1 = a1 * b1 - a2 * b2, r2 = a1 * b2 + a2 * b1;
return to_string(r1) + "+" + to_string(r2) + "i";
}
};
下面这种方法利用到了字符串流类istringstream来读入字符串,直接将实部虚部读入int变量中,注意中间也要把加号读入char变量中,然后再进行运算即可,参见代码如下:
解法二:
class Solution {
public:
string complexNumberMultiply(string a, string b) {
istringstream is1(a), is2(b);
int a1, a2, b1, b2, r1, r2;
char plus;
is1 >> a1 >> plus >> a2;
is2 >> b1 >> plus >> b2;
r1 = a1 * b1 - a2 * b2, r2 = a1 * b2 + a2 * b1;
return to_string(r1) + "+" + to_string(r2) + "i";
}
};
下面这种解法实际上是C语言的解法,用到了sscanf这个读入字符串的函数,需要把string转为cost char*型,然后标明读入的方式和类型,再进行运算即可,参见代码如下:
解法三:
class Solution {
public:
string complexNumberMultiply(string a, string b) {
int a1, a2, b1, b2, r1, r2;
sscanf(a.c_str(), "%d+%di", &a1, &a2);
sscanf(b.c_str(), "%d+%di", &b1, &b2);
r1 = a1 * b1 - a2 * b2, r2 = a1 * b2 + a2 * b1;
return to_string(r1) + "+" + to_string(r2) + "i";
}
};
参考资料:
https://discuss.leetcode.com/topic/84261/java-3-liner
https://discuss.leetcode.com/topic/84382/c-using-stringstream
https://discuss.leetcode.com/topic/84323/java-elegant-solution
https://discuss.leetcode.com/topic/84508/cpp-solution-with-sscanf
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Complex Number Multiplication 复数相乘的更多相关文章
- LeetCode Complex Number Multiplication
原题链接在这里:https://leetcode.com/problems/complex-number-multiplication/description/ 题目: Given two strin ...
- LeetCode 537. 复数乘法(Complex Number Multiplication)
537. 复数乘法 537. Complex Number Multiplication 题目描述 Given two strings representing two complex numbers ...
- LC 537. Complex Number Multiplication
Given two strings representing two complex numbers. You need to return a string representing their m ...
- 【LeetCode】537. Complex Number Multiplication 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...
- [Swift]LeetCode537. 复数乘法 | Complex Number Multiplication
Given two strings representing two complex numbers. You need to return a string representing their m ...
- [LeetCode] Sparse Matrix Multiplication 稀疏矩阵相乘
Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...
- 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 ...
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
随机推荐
- [日常] NOIP 2017滚粗记
突然挑了这么个滑稽的时间补了游记... (成绩日常延时再加上人太菜估计基本上就是颓废记录) 然而文化课太废可能会被强制退役QAQ所以先补了再说吧 day0 一大早被老姚交代了个开十一机房门的任务... ...
- python+selenium:解决上传文件<input type='file'>标签属性被css的visibility隐藏导致无法定位元素的问题
要想上传文件,需要找到在HTML中<input type="file" />这个标签,有它就可以利用send_keys上传文件,不过这里的<input>元素 ...
- alpha-咸鱼冲刺day8-紫仪
总汇链接 一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 正在进行页面整合.然后还有注册跟登陆的功能完善-- 四,问题困难 数据流程大概是搞定了.不过语法不是很熟悉,然后还有各 ...
- Beta No.1
一.今日任务 重新熟悉整体项目 对整个项目在未来的beta冲刺中进程有一个合理的规划 由于我们送出的是一个负责前端的成员,引入的也是一个负责前端工作的女生,(女生做起美工比起男生更加得心应手吧)所以我 ...
- C语言——第二次作业(2)
作业要求一 PTA作业的提交列表 作业要求二 题目1.删除字符串中数字字符(函数题) 1.设计思路 - (1)算法 第一步:调用定义的函数. 第二步:定义i=0.j=0,i为原字符数组角标,j为删除后 ...
- 【iOS】Swift if let 和 if var
if let unwrappedOptional = postDict { print("The optional has a value! It's \(unwrappedOptional ...
- git中级技能
中级技能(上) 一.实验说明 从本节开始,我们会介绍一些中级和高级的用法,这些用法很少用到,前面三节的内容已经满足了日常工作需要,从本节开始的内容可以简单了解,需要的时候再 ...
- bzoj千题计划275:bzoj4817: [Sdoi2017]树点涂色
http://www.lydsy.com/JudgeOnline/problem.php?id=4817 lct+线段树+dfs序 操作1:access 操作2:u到根的-v到根的-lca到根的*2+ ...
- STL之queue
描述 使用STL中的queue,完成入队.出队.获取队首.获取队尾等基本操作. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { queue<int> ...
- 总体来说,require_once 肯定要比 require 性能好
首先,总体来说,require_once 肯定要比 require 性能好. 因为 require 某个文件等同于 "编译 + 执行" 这个文件:require_once 避免了对 ...