class Solution {
public int evalRPN(String[] tokens) {
ArrayDeque<Integer> stack = new ArrayDeque<>();
for (String s : tokens) {
if ("+".equals(s) || "-".equals(s) || "*".equals(s) || "/".equals(s)) {
int b = stack.pop();
int a = stack.pop();
if ("+".equals(s)) {
stack.push(a+b);
} else if ("-".equals(s)) {
stack.push(a-b);
} else if ("*".equals(s)) {
stack.push(a*b);
} else if ("/".equals(s)) {
stack.push(a/b);
}
} else {
stack.push(Integer.valueOf(s));
}
}
return stack.peek();
}
}
/*
้้ต, ๅๅฎ็ตๆ่ฆๆพๅ stack
T: O(n)
S: O(n)
*/