Java 7ms easy to understand solution


  • 4
    S
    public class Solution {
        public String complexNumberMultiply(String a, String b) {
            int[] valA = getValue(a);
            int[] valB = getValue(b);
            
            int real = valA[0] * valB[0] - valA[1] * valB[1];
            int img = valA[0] * valB[1] + valA[1] * valB[0];
            
            return real + "+" + img + "i";
        }
        
        private int[] getValue(String s) {
            String[] str = s.split("\\+");
            int[] val = new int[2];
            val[0] = Integer.valueOf(str[0]);
            int indexOfI = str[1].indexOf("i");
            val[1] = Integer.valueOf(str[1].substring(0, indexOfI));
            
            return val;
        }
    }
    
    

Log in to reply
 

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