-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
29 lines (25 loc) · 980 Bytes
/
Stack.java
File metadata and controls
29 lines (25 loc) · 980 Bytes
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
class Solution {
public int evalRPN(String[] tokens) {
//ok i need to reverse this
//so how to do this
//i will sue stack and if i see operend push into stack
//if i see operator i will pop the two last elemnt and perform the opertor the again push into stack
Stack<Integer>stack = new Stack<>();
for(String token : tokens){
if("+-*/".contains(token)&&token.length()==1){
int a = stack.pop();
int b = stack.pop();
switch(token){
case "+"->stack.push(b+a);
case "-"->stack.push(b-a);
case "*"->stack.push(b*a);
case "/"->stack.push(b/a);
}
}else{ //if not operatore add into stack
stack.push(Integer.parseInt(token));
}
}
//top res return
return stack.pop();
}
}