-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvaluateReversePolishNotation.java
More file actions
53 lines (41 loc) · 1.77 KB
/
EvaluateReversePolishNotation.java
File metadata and controls
53 lines (41 loc) · 1.77 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Evaluate the value of an arithmetic expression in Reverse Polish Notation.
// Valid operators are +, -, *, /. Each operand may be an integer or another expression.
// See: https://leetcode.com/problems/evaluate-reverse-polish-notation/
package leetcode.stack;
import java.util.Stack;
public class EvaluateReversePolishNotation {
public int evalRPN(String[] tokens) {
Stack<Integer> s = new Stack<>();
for (String token : tokens) {
if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")) {
int secondOperand = s.pop();
int firstOperand = s.pop();
switch (token) {
case "+":
s.push(firstOperand + secondOperand);
break;
case "-":
s.push(firstOperand - secondOperand);
break;
case "*":
s.push(firstOperand * secondOperand);
break;
case "/":
s.push(firstOperand / secondOperand);
}
} else {
s.push(Integer.parseInt(token));
}
}
return s.pop();
}
public static void main(String[] args) {
EvaluateReversePolishNotation sln = new EvaluateReversePolishNotation();
String[] tokens1 = new String[] { "10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+" };
System.out.println(sln.evalRPN(tokens1)); // 22
String[] tokens2 = new String[] { "4", "13", "5", "/", "+" };
System.out.println(sln.evalRPN(tokens2)); // 6
String[] tokens3 = new String[] { "2", "1", "+", "3", "*" };
System.out.println(sln.evalRPN(tokens3)); // 9
}
}