AC Java solution without using split()


  • 0

    '''

    public String complexNumberMultiply(String a, String b) {
        int w = 0;
        int z = 0;
        int x = 0;
        int y = 0;
        int start = 0;
        for (int i = 0; i < a.length(); i++){
            if (a.charAt(i) == '+'){
                w = convert(start, i, a);
                start = i + 1;
            }
            if (a.charAt(i) == 'i'){
                x = convert(start, i, a);
                start = 0;
            }
        }
        for (int i = 0; i < b.length(); i++){
            if (b.charAt(i) == '+'){
                y = convert(start, i, b);
                start = i + 1;
            }
            if (b.charAt(i) == 'i'){
                z = convert(start, i, b);
                start = 0;
            }
        }
        return (w * y - x * z) + "+" + (w * z + x * y) + "i";
    }
    private int convert(int i, int j, String s){
        int n = 0;
        boolean flag = true;
        if (s.charAt(i) == '-'){
            flag = false;
            i++;
        }
        while (i < j){
            n = n * 10 + (s.charAt(i) - '0');
            i++;
        }
        return flag ? n : 0 - n;
    }
    

    '''


Log in to reply
 

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