use stack to calculate-Simple Java Solution


  • 0
    Z
    import java.util.LinkedList;
    
    public class Solution {
        /**
         * a < b return -1;
         * a = b return 0;
         * a > b return 1;
         */
        public int getPriority(char a, char b) {
            int result = 0;
            switch (b) {
                case '+':
                case '-':
                    if (a == '#') {
                        result = -1;
                    } else {
                        result = 1;
                    }
                    break;
                case '*':
                case '/':
                    if (a == '*' || a == '/') {
                        result = 1;
                    } else {
                        result = -1;
                    }
                    break;
                case '#':
                    if (a == '#') {
                        result = 0;
                    } else {
                        result = 1;
                    }
            }
            return result;
        }
    
        public int cal(char op, int num1, int num2) {
            int result = 0;
            switch (op) {
                case '+':
                    result = num1 + num2;
                    break;
                case '-':
                    result = num1 - num2;
                    break;
                case '*':
                    result = num1 * num2;
                    break;
                case '/':
                    result = num1 / num2;
            }
            return result;
        }
    
        public int calculate(String s) {
            if (s == null || s.trim().isEmpty()) {
                return 0;
            }
    
            LinkedList<Integer> numStack = new LinkedList<>();
            LinkedList<Character> opStack = new LinkedList<>();
            opStack.push('#');
            s = s + "#";
            int i = 0;
            while (!(s.charAt(i) == '#' && opStack.peek() == '#')) {
                if (Character.isSpaceChar(s.charAt(i))) {
                    i++;
                    continue;
                }
    
                if (Character.isDigit(s.charAt(i))) {
                    int sum = 0;
                    while (Character.isDigit(s.charAt(i))) {
                        sum = sum * 10 + (s.charAt(i) - '0');
                        i++;
                    }
                    numStack.push(sum);
                } else {
                    int compareResult = getPriority(opStack.peek(), s.charAt(i));
                    if (compareResult < 0) {
                        opStack.push(s.charAt(i));
                        i++;
                    } else if (compareResult == 0) {
                        opStack.pop();
                        i++;
                    } else {
                        int num2 = numStack.pop();
                        int num1 = numStack.pop();
                        numStack.push(cal(opStack.pop(), num1, num2));
                    }
                }
            }
            return numStack.pop();
        }
    
        public static void main(String[] args) {
            String s = " 3+5 / 2 ";
            int result = new Solution().calculate(s);
            System.out.println(result);
        }
    }
    

Log in to reply
 

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