Skip to content

Commit 59959e4

Browse files
refactor 150
1 parent 1bdba99 commit 59959e4

File tree

1 file changed

+20
-70
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+20
-70
lines changed
Lines changed: 20 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,37 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.Arrays;
34
import java.util.HashSet;
45
import java.util.Set;
56
import java.util.Stack;
67

7-
/**
8-
* 150. Evaluate Reverse Polish Notation
9-
*
10-
* Evaluate the value of an arithmetic expression in Reverse Polish Notation.
11-
* Valid operators are +, -, *, /. Each operand may be an integer or another expression.
12-
*
13-
* Note:
14-
* Division between two integers should truncate toward zero.
15-
* The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.
16-
*
17-
* Example 1:
18-
* Input: ["2", "1", "+", "3", "*"]
19-
* Output: 9
20-
* Explanation: ((2 + 1) * 3) = 9
21-
*
22-
* Example 2:
23-
* Input: ["4", "13", "5", "/", "+"]
24-
* Output: 6
25-
* Explanation: (4 + (13 / 5)) = 6
26-
*
27-
* Example 3:
28-
* Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
29-
* Output: 22
30-
* Explanation:
31-
* ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
32-
* = ((10 * (6 / (12 * -11))) + 17) + 5
33-
* = ((10 * (6 / -132)) + 17) + 5
34-
* = ((10 * 0) + 17) + 5
35-
* = (0 + 17) + 5
36-
* = 17 + 5
37-
* = 22
38-
*/
398
public class _150 {
409

4110
public static class Solution1 {
4211
public int evalRPN(String[] tokens) {
43-
Stack<String> stack = new Stack<>();
44-
Set<String> op = new HashSet();
45-
op.add("+");
46-
op.add("-");
47-
op.add("*");
48-
op.add("/");
49-
50-
int exp = 0;
51-
String operand1 = "";
52-
String operand2 = "";
53-
for (int i = 0; i < tokens.length; ) {
54-
while ((i < tokens.length) && (!op.contains(tokens[i]))) {
55-
stack.push(tokens[i]);
56-
i++;
57-
}
58-
if (i == tokens.length) {
59-
if (!stack.isEmpty()) {
60-
return Integer.parseInt(stack.pop());
61-
}
62-
} else if (op.contains(tokens[i])) {
63-
if (!stack.isEmpty()) {
64-
operand2 = stack.pop();
65-
}
66-
if (!stack.isEmpty() && !op.contains(stack.peek())) {
67-
operand1 = stack.pop();
68-
}
69-
if (tokens[i].equals("+")) {
70-
exp = Integer.parseInt(operand1) + Integer.parseInt(operand2);
71-
} else if (tokens[i].equals("-")) {
72-
exp = Integer.parseInt(operand1) - Integer.parseInt(operand2);
73-
} else if (tokens[i].equals("*")) {
74-
exp = Integer.parseInt(operand1) * Integer.parseInt(operand2);
75-
} else if (tokens[i].equals("/")) {
76-
exp = Integer.parseInt(operand1) / Integer.parseInt(operand2);
12+
Set<String> operators = new HashSet<>(Arrays.asList("+", "-", "*", "/"));
13+
Stack<Integer> stack = new Stack<>();
14+
for (String token : tokens) {
15+
if (operators.contains(token)) {
16+
int secondNum = stack.pop();
17+
int firstNum = stack.pop();
18+
int result;
19+
if (token.equals("+")) {
20+
result = firstNum + secondNum;
21+
} else if (token.equals("-")) {
22+
result = firstNum - secondNum;
23+
} else if (token.equals("*")) {
24+
result = firstNum * secondNum;
7725
} else {
78-
exp = Integer.parseInt(operand2);
26+
result = firstNum / secondNum;
7927
}
80-
stack.push(String.valueOf(exp));
81-
i++;
28+
stack.push(result);
29+
} else {
30+
int num = Integer.parseInt(token);
31+
stack.push(num);
8232
}
8333
}
84-
return Integer.parseInt(stack.pop());
34+
return stack.pop();
8535
}
8636
}
8737
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy