class Solution { public int evalRPN(String[] tokens) { // 遇到数字则入栈;遇到算符则取出栈顶两个数字进行计算,并将结果压入栈中。 Stack<Integer> stack = new Stack<>(); int num; for (String s : tokens) { switch (s) { case "+": stack.push(stack.pop() + stack.pop()); break; case "-": num = stack.pop(); stack.push(stack.pop() - num); break; case "*": stack.push(stack.pop() * stack.pop()); break; case "/": num = stack.pop(); stack.push(stack.pop() / num); break; default: stack.push(Integer.valueOf(s)); } } return stack.pop(); } }