537. Complex Number Multiplication

xiaoxiao2021-02-27  318

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. code: class Solution(object):     def complexNumberMultiply(self, a, b):         """         :type a: str         :type b: str         :rtype: str         """         a1=int(list(a.split("+"))[0])         a2=int(list(a.split("+"))[-1][:-1])         b1=int(list(b.split("+"))[0])         b2=int(list(b.split("+"))[-1][:-1])         c=a1*b1-a2*b2         d=b1*a2+a1*b2         return  "%d+%di"%(c,d) 用map: a1, a2 = map(int, a[:-1].split('+')) b1, b2 = map(int, b[:-1].split('+')) return '%d+%di' % (a1 * b1 - a2 * b2, a1 * b2 + a2 * b1)
转载请注明原文地址: https://www.6miu.com/read-5500.html

最新回复(0)