150. 逆波兰表达式求值

137 阅读1分钟

题目

image.png

思路

image.png

代码

class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].equals("+")) {//别写成==
                stack.push(stack.pop() + stack.pop());
            } else if (tokens[i].equals("-")) {
                int num2 = stack.pop(), num1 = stack.pop();//注意顺序
                stack.push(num1 - num2);
            } else if (tokens[i].equals("*")) {
                stack.push(stack.pop() * stack.pop());
            } else if (tokens[i].equals("/")) {
                int num2 = stack.pop(), num1 = stack.pop();//注意顺序
                stack.push(num1 / num2);
            } else {//为数字,直接压栈
                stack.push(Integer.parseInt(tokens[i]));
            }
        }
        int res = 0;
        while (!stack.isEmpty()) {
            res += stack.pop();
        }
        return res;
    }
}