class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String token : tokens) {
if (isNumber(token)) {
stack.push(Integer.parseInt(token));
} else {
int afterNum = stack.pop();
int beforeNum = stack.pop();
if (token.equals("+")) {
stack.push(beforeNum + afterNum);
} else if (token.equals("-")) {
stack.push(beforeNum - afterNum);
} else if (token.equals("*")) {
stack.push(beforeNum * afterNum);
} else {
stack.push(beforeNum / afterNum);
}
}
}
return stack.pop();
}
// str이 숫자인지 판단
private static boolean isNumber(String str) {
if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")) {
return false;
}
return true;
}
}