思路:
// @Title: 逆波兰表达式求值 (Evaluate Reverse Polish Notation)
// @Author: qisiii
// @Date: 2024-09-14 13:21:52
// @Runtime: 7 ms
// @Memory: 43.6 MB
// @comment:
// @flag:
class Solution {
public int evalRPN(String[] tokens) {
Stack<String> stack = new Stack<>();
for (String token : tokens) {
if (token.equals("+")) {
Integer second = Integer.valueOf(stack.pop());
Integer first = Integer.valueOf(stack.pop());
stack.push(String.valueOf(first + second));
} else if (token.equals("-")) {
Integer second = Integer.valueOf(stack.pop());
Integer first = Integer.valueOf(stack.pop());
stack.push(String.valueOf(first - second));
} else if (token.equals("*")) {
Integer second = Integer.valueOf(stack.pop());
Integer first = Integer.valueOf(stack.pop());
stack.push(String.valueOf(first * second));
} else if (token.equals("/")) {
Integer second = Integer.valueOf(stack.pop());
Integer first = Integer.valueOf(stack.pop());
stack.push(String.valueOf(first / second));
} else {
stack.push(token);
}
}
return Integer.valueOf(stack.peek());
}
}