Share No Stack Java Solution, beat 97.30%


  • 1
    F

    Basically my algorithm utilize the idea that only two levels of operations are involved in the calculation without parenthesis. Save level 1 calculation and level 2 calculation separately.

      public int calculate(String s) {
            char ops = '+';
            int sign = 1, curr = 0, prev = 0, val = 0;
            for(int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c >= 48 && c <= 57) {
                    val = val*10 + (c - 48);
                    continue;
                } else if(c == ' ') continue;
                curr = getCurrentVal(ops, val, curr);
                if(c == '+' || c == '-') {
                    prev += curr * sign;
                    sign = (c == '+')? 1: -1;
                } else if(ops == '+' || ops == '-') curr = val;
                ops = c;
                val = 0;
            }
            return prev + getCurrentVal(ops, val, curr) * sign;
        }
        private int getCurrentVal(char ops, int val, int curr) {
            if(ops == '*') curr *= val;
            else if(ops == '/') curr /= val;
            else curr = val;
            return curr;
        }

Log in to reply
 

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