题:使用字符串表示复数,返回两个复数的乘积。
解:考察复数乘法。
class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
def get_real_i(nums:str):
#give str "a+bi" return a,b
i = nums.index('+')
a = int(nums[:i])
b = int(nums[i+1:-1])
return a,b
a1,b1 =get_real_i(num1)
a2,b2 =get_real_i(num2)
a = a1*a2 - b1*b2
b = a1*b2 + a2*b1
return f"{a}+{b}i"