解题思路:
字符串分割的典型题目,直接用python的split对字符串进行分割,获取实数和虚数部分,转为int型进行乘积,最后再将结果转换为字符串返回,代码如下:
class Solution(object):
def complexNumberMultiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
a1 = int(num1.split('+')[0])
b1 = int((num1.split('+')[1]).split('i')[0])
a2 = int(num2.split('+')[0])
b2 = int((num2.split('+')[1]).split('i')[0])
return str(a1 * a2 - b1 * b2) + '+' + str(a1 * b2 + a2 * b1) + 'i'