use StringBuilder of Java.


  • 0
    S
        public String complexNumberMultiply(String a, String b) {
            StringBuilder sb = new StringBuilder();
            String[] parta = a.split("\\+");
            String[] partb = b.split("\\+");
            
            int a1 = Integer.parseInt(parta[0]);
            int b1 = Integer.parseInt(parta[1].substring(0, parta[1].length() - 1));
            
            int a2 = Integer.parseInt(partb[0]);
            int b2 = Integer.parseInt(partb[1].substring(0, partb[1].length() - 1));
            
            int res_a = a1 * a2 - b1 * b2;
            sb.append(res_a);
            sb.append("+");
            int res_b = a1 * b2 + a2 * b1;
            sb.append(res_b);
            sb.append("i");
            
            return sb.toString();
            
        }
    

Log in to reply
 

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