Solution with JS


  • 0
    G

    Idea:
    (a1+b1i)(a2+b2i)=(a1a2-b1b2) +(a1b2+a2b1)*i

    /**
     * @param {string} a
     * @param {string} b
     * @return {string}
     */
    var complexNumberMultiply = function(a, b) {
        var m=[a,b].map(str=>/(-?\d+)\+(-?\d+)i/.exec(str));
        var [a1,b1,a2,b2]=[m[0][1],m[0][2],m[1][1],m[1][2]];
        var real=a1*a2-b1*b2;
        var fake=a1*b2+a2*b1;
        return ''+real+"+"+fake+"i";
    };
    

Log in to reply
 

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