Skip to content

Commit 68163cc

Browse files
Evaluate reverse polish notation (#171)
* Solution of Evaluate Reverse Polish Notation * Update evaluate_reverse_polish_notation.java * update readme.md
1 parent 154068e commit 68163cc

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
3+
class Solution {
4+
public int evalRPN(String[] tokens) {
5+
Stack<Integer> st = new Stack<>();
6+
int a,b;
7+
for(String s: tokens){
8+
if(s.equals("+")){
9+
st.add(st.pop()+st.pop());
10+
}
11+
else if(s.equals("*")){
12+
st.add(st.pop() * st.pop());
13+
}
14+
else if(s.equals("/")){
15+
b=st.pop();
16+
a = st.pop();
17+
st.add(a/b);
18+
}
19+
else if(s.equals("-")){
20+
b = st.pop();
21+
a = st.pop();
22+
st.add(a-b);
23+
}
24+
else{
25+
st.add(Integer.parseInt(s));
26+
}
27+
}
28+
return st.pop();
29+
}
30+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
198198
| ---- | ------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- | ------ | ------ | ---------- | ---------------------- | ---- |
199199
| 020 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Python](./Python/20_ValidParentheses.py) | _O(n)_ | _O(n)_ | Easy | Stack | |
200200
| 084 | [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) | [C++](./C++/Largest-Rectangle-in-Histogram.cpp) | _O(n)_ | _O(n)_ | Hard | Stack |
201-
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Python](./Python/150.EvaluateReversePolishNotation.py) | _O(n)_ | _O(1)_ | Medium | Stack | |
201+
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Python](./Python/150.EvaluateReversePolishNotation.py) <br> [Java](./Java/evaluate_reverse_polish_notation.java) | _O(n)_ | _O(1)_ | Medium | Stack | |
202202
| 1047 | [Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [C++](./C++/remove-all-adjacent-duplicates-in-string.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | |
203203
| 682 | [Baseball Game](https://leetcode.com/problems/baseball-game/) | [C++](./C++/Baseball-Game.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | |
204204
| 1381 | [Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/) | [Java](./Java/Design-a-Stack-With-Increment-Operation.java) | _O(n)_ | _O(n)_ | Medium | Stack | |

0 commit comments

Comments
 (0)