-
Notifications
You must be signed in to change notification settings - Fork 0
/
LC150.java
executable file
·52 lines (47 loc) · 1.76 KB
/
LC150.java
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
import java.util.Stack;
public class LC150 {
/*
* Input: tokens = ["2","1","+","3","*"]
* Output: 9
* Explanation: ((2 + 1) * 3) = 9
*/
public static int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
int numbr, num1, num2;
for (String str : tokens) {
if (!str.equals("+") && !str.equals("-") && !str.equals("*") && !str.equals("/")) {
numbr = Integer.parseInt(str);
stack.push(numbr);
} else {
if (!stack.isEmpty()) {
num1 = stack.pop();
num2 = stack.pop();
System.out.println(num1 + "+ " + num2);
switch (str) {
case "+":
stack.push(num1 + num2);
break;
case "-":
stack.push(num1 - num2);
break;
case "*":
stack.push(num1 * num2);
break;
case "/":
stack.push(num1 / num2);
System.out.println(num1 + "+ " + num2);
System.out.println("peek" + stack.peek());
System.out.println("div" + num1 / num2);
break;
}
// System.out.println(stack.peek());
}
}
}
return stack.pop();
}
public static void main(String[] args) {
String[] tokens = { "4", "13", "5", "/", "+" };
System.out.println(evalRPN(tokens));
}
}