3-lines Python with explanation


  • 0
    T
    class Solution(object):
        def complexNumberMultiply(self, a, b):
            a_s, b_s = a.strip('i').split('+'), b.strip('i').split('+')
            ar, ai, br, bi = int(a_s[0]), int(a_s[1]),  int(b_s[0]), int(b_s[1]) #unnecessary, for clarity
            return str(ar*br-ai*bi)+"+"+str(ar*bi+ai*br)+"i"
    
    1. Get rid of 'i' at the back and split each number into real&imaginary parts by '+'
    2. convert the number strings to int and calculate, return the string result

Log in to reply
 

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