forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
33 lines (33 loc) · 913 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public int evalRPN(String[] tokens) {
Deque<Integer> s = new ArrayDeque<>();
int left, right;
for (String token : tokens) {
switch(token) {
case "+":
right = s.pop();
left = s.pop();
s.push(left + right);
break;
case "-":
right = s.pop();
left = s.pop();
s.push(left - right);
break;
case "*":
right = s.pop();
left = s.pop();
s.push(left * right);
break;
case "/":
right = s.pop();
left = s.pop();
s.push(left / right);
break;
default:
s.push(Integer.valueOf(token));
}
}
return s.pop();
}
}