-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathevaluateReversePolishNotation.java
More file actions
31 lines (31 loc) · 1.06 KB
/
Copy pathevaluateReversePolishNotation.java
File metadata and controls
31 lines (31 loc) · 1.06 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
public class Solution {
public int evalRPN(String[] tokens) {
int result = 0;
Stack stack = new Stack();
for(int i=0;i<tokens.length;i++) {
//operator
if(tokens[i].equals("+")==true||tokens[i].equals("-")==true||tokens[i].equals("*")==true||tokens[i].equals("/")==true) {
int tmp = (int)stack.pop();
int tmp1 = (int)stack.pop();
switch(tokens[i].charAt(0)) {
case '+' :
stack.push(tmp1+tmp);
break;
case '-' :
stack.push(tmp1-tmp);
break;
case '*' :
stack.push(tmp1*tmp);
break;
case '/' :
stack.push(tmp1/tmp);
break;
}
} else {
stack.push(Integer.parseInt(tokens[i]));
}
}
result = (int)stack.pop();
return result;
}
}