My readable Python solution using cross product


  • 0
    class Solution(object):
        def complexNumberMultiply(self, a, b):
            """
            :type a: str
            :type b: str
            :rtype: str
            """
            aReal, aImag = a.split('+')
            bReal, bImag = b.split('+')
            aReal = int(aReal)
            bReal = int(bReal)
            aImag = int(aImag[:-1])
            bImag = int(bImag[:-1])
            
            oReal = aReal*bReal - aImag*bImag
            oImag = aReal*bImag + bReal*aImag
            return str(oReal) + '+' + str(oImag) + 'i'
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.