Java, easy to understand, 13ms


  • 0
    A
    public String complexNumberMultiply(String a, String b) {
        String[] numsA = a.split("\\+|i");
        String[] numsB = b.split("\\+|i");
        
        int[] nums = new int[4];
        nums[0] = Integer.parseInt(numsA[0]);
        nums[1] = Integer.parseInt(numsA[1]);
        nums[2] = Integer.parseInt(numsB[0]);
        nums[3] = Integer.parseInt(numsB[1]);
        
        //(a+bi)*(c+di) = (ac - bd) + (ad + bc)i
        int real = nums[0] * nums[2] - nums[1] * nums[3];
        int im = nums[0] * nums[3] + nums[1] * nums[2];
        
        return real + "+" + im + "i";
     }

Log in to reply
 

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