Skip to content

Commit 8b4fdc8

Browse files
solves reverse polish notation in java
1 parent 091e335 commit 8b4fdc8

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache) | [![Java](assets/java.png)](src/LRUCache.java) | |
134134
| 147 | [Insertion Sort List](https://leetcode.com/problems/insertion-sort-list) | [![Java](assets/java.png)](src/InsertionSortList.java) | |
135135
| 148 | [Sort List](https://leetcode.com/problems/sort-list) | [![Java](assets/java.png)](src/SortList.java) | |
136-
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation) | | |
136+
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation) | [![Java](assets/java.png)](src/EvaluateReversePolishNotation.java) | |
137137
| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string) | | |
138138
| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray) | | |
139139
| 153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array) | | |

Diff for: src/EvaluateReversePolishNotation.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// https://leetcode.com/problems/evaluate-reverse-polish-notation
2+
// T: O(N)
3+
// S: O(N)
4+
5+
import java.util.Set;
6+
import java.util.Stack;
7+
8+
public class EvaluateReversePolishNotation {
9+
private static final String ADDITION = "+";
10+
private static final String SUBTRACTION = "-";
11+
private static final String MULTIPLICATION = "*";
12+
private static final String DIVISION = "/";
13+
14+
private static final Set<String> OPERATORS = Set.of(
15+
ADDITION,
16+
SUBTRACTION,
17+
MULTIPLICATION,
18+
DIVISION
19+
);
20+
21+
public static int evalRPN(String[] tokens) {
22+
Stack<Integer> stack = new Stack<>();
23+
for (String token : tokens) {
24+
if (isOperator(token)) {
25+
int number1 = stack.pop();
26+
int number2 = stack.pop();
27+
stack.push(apply(number2, number1, token));
28+
} else {
29+
stack.push(Integer.parseInt(token));
30+
}
31+
}
32+
return stack.pop();
33+
}
34+
35+
private static boolean isOperator(String token) {
36+
return OPERATORS.contains(token);
37+
}
38+
39+
private static int apply(int a, int b, String operator) {
40+
return switch (operator) {
41+
case ADDITION -> a + b;
42+
case SUBTRACTION -> a - b;
43+
case MULTIPLICATION -> a * b;
44+
case DIVISION -> a / b;
45+
default -> 0;
46+
};
47+
}
48+
}

Diff for: src/HelloWorld.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
public class HelloWorld {
22
public static void main(String[] args) {
3+
System.out.println(Integer.parseInt("2"));
4+
System.out.println(Integer.parseInt("-11"));
5+
System.out.println(EvaluateReversePolishNotation.evalRPN(new String[] {"2", "-11", "-"}));
36
}
47
}

0 commit comments

Comments
 (0)