Java solution sharing


  • 0
    L
    public class Solution {
        public String complexNumberMultiply(String a, String b) {
            int[] aArray = split(a);
            int[] bArray = split(b);
            
            int result1 = aArray[0]*bArray[0] - aArray[1]*bArray[1];
            int result2 = aArray[1]*bArray[0] + aArray[0]*bArray[1];
            
            StringBuilder sb = new StringBuilder();
            sb.append(result1).append('+').append(result2).append('i');
            return sb.toString();
        }
        
        private int[] split(String a){
            int index = a.indexOf("+");
            int solid = Integer.parseInt(a.substring(0, index));
            int non_solid = Integer.parseInt(a.substring(index+1, a.length()-1));
            
            return new int[]{solid, non_solid};
        }
    }
    

Log in to reply
 

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