-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_150_evalRPN.java
More file actions
37 lines (36 loc) · 1.03 KB
/
Copy path_150_evalRPN.java
File metadata and controls
37 lines (36 loc) · 1.03 KB
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
34
35
36
37
import java.util.ArrayDeque;
import java.util.Deque;
public class _150_evalRPN {
public int evalRPN(String[] tokens) {
Deque<Integer> stack = new ArrayDeque<>();
for(String s:tokens){
switch(s){
case "+":{
stack.push(stack.pop() + stack.pop());
break;
}
case "-":{
int b = stack.pop();
int a = stack.pop();
stack.push(a - b);
break;
}
case "*":{
stack.push(stack.pop()*stack.pop());
break;
}
case "/":{
int b = stack.pop();
int a = stack.pop();
stack.push(a/b);
break;
}
default:{
int n = Integer.parseInt(s);
stack.push(n);
}
}
}
return stack.pop();
}
}