File tree 3 files changed +52
-1
lines changed
3 files changed +52
-1
lines changed Original file line number Diff line number Diff line change 133
133
| 146 | [ LRU Cache] ( https://leetcode.com/problems/lru-cache ) | [ ![ Java] ( assets/java.png )] ( src/LRUCache.java ) | |
134
134
| 147 | [ Insertion Sort List] ( https://leetcode.com/problems/insertion-sort-list ) | [ ![ Java] ( assets/java.png )] ( src/InsertionSortList.java ) | |
135
135
| 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 ) | |
137
137
| 151 | [ Reverse Words in a String] ( https://leetcode.com/problems/reverse-words-in-a-string ) | | |
138
138
| 152 | [ Maximum Product Subarray] ( https://leetcode.com/problems/maximum-product-subarray ) | | |
139
139
| 153 | [ Find Minimum in Rotated Sorted Array] ( https://leetcode.com/problems/find-minimum-in-rotated-sorted-array ) | | |
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
public class HelloWorld {
2
2
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" , "-" }));
3
6
}
4
7
}
You can’t perform that action at this time.
0 commit comments