We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 601d4ee commit 838ffb5Copy full SHA for 838ffb5
1 file changed
Stacks/Problems/ReversePolishNotation.java
@@ -0,0 +1,32 @@
1
+class ReversePolishNotation { // Postfix
2
+ public int evalRPN(String[] tokens) {
3
+ Stack<Integer> stk = new Stack<>();
4
+
5
+ for(String i : tokens){
6
+ if(i.equals("+")){
7
+ int b = stk.pop();
8
+ int a = stk.pop();
9
+ stk.push(a+b);
10
+ }
11
+ else if(i.equals("-")){
12
13
14
+ stk.push(a-b);
15
16
+ else if(i.equals("*")){
17
18
19
+ stk.push(a*b);
20
21
+ else if(i.equals("/")){
22
23
24
+ stk.push(a/b);
25
26
+ else{
27
+ stk.push(Integer.parseInt(i));
28
29
30
+ return stk.pop();
31
32
+}
0 commit comments