Java naive solution


  • 0
    public String complexNumberMultiply(String a, String b) {
        int [] numberA = parseComplex(a);
        int [] numberB = parseComplex(b);
        int real = 0;
        int abst = 0;
        real += numberA[0]*numberB[0] - numberA[1]*numberB[1];
        abst += numberA[0]*numberB[1] + numberA[1]*numberB[0];
        StringBuffer ret = new StringBuffer();
        ret.append(real);
        ret.append("+");
        ret.append(abst);
        ret.append("i");
        return ret.toString();
    }
    private int[] parseComplex(String a){
        int [] ret = new int [2];
        String[] strs = a.split("\\+");
        ret[0] = Integer.parseInt(strs[0]);
        ret[1] = Integer.parseInt(strs[1].substring(0,strs[1].length()-1));
        return ret;
    }

Log in to reply
 

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