You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- 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.
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:
2
Example 2:
2
Note:
这道题让我们求复数的乘法,有关复数的知识最早还是在本科的复变函数中接触到的,难起来还真是难。但是这里只是最简单的乘法,只要利用好定义i2=-1就可以解题,而且这道题的另一个考察点其实是对字符的处理,我们需要把字符串中的实部和虚部分离开并进行运算,那么我们可以用STL中自带的find_last_of函数来找到加号的位置,然后分别拆出实部虚部,进行运算后再变回字符串,参见代码如下:
解法一:
下面这种方法利用到了字符串流类istringstream来读入字符串,直接将实部虚部读入int变量中,注意中间也要把加号读入char变量中,然后再进行运算即可,参见代码如下:
解法二:
下面这种解法实际上是C语言的解法,用到了sscanf这个读入字符串的函数,需要把string转为cost char*型,然后标明读入的方式和类型,再进行运算即可,参见代码如下:
解法三:
参考资料:
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 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: