Java beats(95.88%)


  • 0
    S
    public int calculate(String s) {
            char[] c = s.toCharArray();
            int[] num = new int[c.length];
            char[] symb = new char[c.length / 2];
            int head = 0, end = c.length, end1 = 0, end2 = 0;
            while(head < end && c[head] == ' '){
                head++;
            }
            while(head < end && c[head] <= '9' && c[head] >= '0'){
                num[end1] = num[end1] * 10 + (c[head++] - '0');
            }
            end1++;
            while(head < end){
                while(head < end && c[head] == ' '){
                    head++;
                }
                if(head >= end) break;
                int tempt = head++;
                while(head < end && c[head] == ' '){
                    head++;
                }
                int t = 0;
                while(head < end && c[head] <= '9' && c[head] >= '0'){
                    t = t * 10 + (c[head++] - '0');
                }
                num[end1++] = t;
                if(c[tempt] == '*'){
                    num[end1 - 2] *= num[end1 - 1];
                    end1--;
                }
                else if(c[tempt] == '/'){
                    num[end1 - 2] = num[end1 - 2] / num[end1 - 1];
                    end1--;
                }
                else symb[end2++] = c[tempt];
            }
            for(int i = 0; i < end2; i++){
                if(symb[i] == '+')  num[i + 1] += num[i];
                else num[i+1] = num[i] - num[i+1];
            }
            return num[end2];
        }
    

Log in to reply
 

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