A Python solution via mathematical explanation


  • 0
    Y

    Here we have two complex numbers represented as a+bi and c+di.
    By the rule of polynomial multiplications, the result will look like (ac-bd)+(ad+bc)i.
    So this will be a new A+Bi which I represented in res_a+res_bi in below code.

    def complexNumberMultiply(self, a, b):
        m = a; n = b
        
        a, b = m.replace("i", "").split("+")
        c, d = n.replace("i", "").split("+")
        
        a, b, c, d = int(a), int(b), int(c), int(d)
        
        res_a = a*c-b*d
        res_b = a*d+b*c
        
        res = str(res_a) + "+" + str(res_b) + "i"
        
        return res
    

Log in to reply
 

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