被上个medium吓到,这个很简单86.25%
note:print int('-1') #-1
print str(-1) #'-1'
class Solution(object):
def complexNumberMultiply(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
def trans(string):
a,bb = string.split('+')
b = bb.split('i')[0]
#a1 = ((-1)**(len(a)))*(-1)
#b1 = ((-1)**(len(b)))*(-1)
return int(a),int(b)
a1,b1 = trans(a)
a2,b2 = trans(b)
a3 = str(a1*a2-b1*b2)
b3 = str(a1*b2+a2*b1)
return a3+'+'+b3+'i'
-------------------------------
97.25% 妙啊,也没用到正则 还是要掌握特征 以及学习一下返回的格式
class Solution(object):
def complexNumberMultiply(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
a1,b1 = map(int, a[:-1].split('+'))
a2,b2 = map(int, b[:-1].split('+'))
return '%d+%di'%(a1*a2-b1*b2, a1*b2+a2*b1)
-----------------------------------
另一种返回的格式 86.25%
class Solution(object):
def complexNumberMultiply(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
a1,b1 = map(int, a[:-1].split('+'))
a2,b2 = map(int, b[:-1].split('+'))
return '{i}+{j}i'.format(i=a1*a2-b1*b2, j=a1*b2+a2*b1)