["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
public class Solution {
public int evalRPN(String[] tokens) {
int num1 = 0, num2 = 0;
Stack<Integer> stack = new Stack<>();
for (String token : tokens) {
switch(token) {
case "+":
num1 = stack.pop();
num2 = stack.pop();
stack.push(num1 + num2);
break;
case "-":
num1 = stack.pop();
num2 = stack.pop();
stack.push(num2 - num1);
break;
case "*":
num1 = stack.pop();
num2 = stack.pop();
stack.push(num1 * num2);
break;
case "/":
num1 = stack.pop();
num2 = stack.pop();
stack.push(num2 / num1);
break;
default:
stack.push(Integer.parseInt(token));
}
}
return stack.peek();
}
}