537. Complex Number Multiplication

Posted swallowblank

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了537. Complex Number Multiplication相关的知识,希望对你有一定的参考价值。

题目大意:

    给出a, b两个用字符串表示的虚数,求a*b

 

题目思路:

    偷了个懒,Python3的正则表达式匹配了一下,当然acm里肯定是不行的

 

 1 class Solution:
 2     def complexNumberMultiply(self, a, b):
 3         """
 4         :type a: str
 5         :type b: str
 6         :rtype: str
 7         """
 8         match = re.search(r\'([+-]*\\d+)[+-]([+-]*\\d+)i$\', a)
 9         x1 = int(match.group(1))
10         y1 = int(match.group(2))
11         match = re.search(r\'([+-]*\\d+)[+-]([+-]*\\d+)i$\', b)
12         x2 = int(match.group(1))
13         y2 = int(match.group(2))
14         ans = \'\'
15         ans = ans + str(x1*x2 - y1*y2) + \'+\'
16         ans = ans + str(x1*y2 + x2*y1) + \'i\'
17         return ans
18         

 

以上是关于537. Complex Number Multiplication的主要内容,如果未能解决你的问题,请参考以下文章