Java Concise and Easy Solution


  • 0
    public class Solution {
        public String complexNumberMultiply(String a, String b) {
            String[] aa = a.split("\\+");
            String[] bb = b.split("\\+");
            int a_re = Integer.parseInt(aa[0]);
            int b_re = Integer.parseInt(bb[0]);
            int a_im = Integer.parseInt(aa[1].substring(0, aa[1].length() - 1));
            int b_im = Integer.parseInt(bb[1].substring(0, bb[1].length() - 1));
            int res_re = a_re * b_re - a_im * b_im;
            int res_im = a_re * b_im + b_re * a_im;
            
            return res_re + "+" + res_im + "i";
        }
    }
    

Log in to reply
 

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