Skip to content

Commit 3e2387b

Browse files
fix 224 and add tests
1 parent 45aa87c commit 3e2387b

File tree

2 files changed

+101
-84
lines changed

2 files changed

+101
-84
lines changed
Lines changed: 55 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,74 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
import java.util.Stack;
3+
import java.util.Deque;
4+
import java.util.LinkedList;
65

76
public class _224 {
87

98
public static class Solution1 {
10-
9+
/**
10+
* My complete original solution on 12/23/2021
11+
*/
1112
public int calculate(String s) {
12-
if (s == null || s.isEmpty()) {
13-
return 0;
14-
}
15-
16-
s = s.replaceAll("\\s", "");
17-
char[] chars = s.toCharArray();
18-
List<String> filteredStr = new ArrayList();
19-
for (int i = 0; i < chars.length; ) {
20-
StringBuilder sb = new StringBuilder();
21-
while (i < chars.length && Character.isDigit(chars[i])) {
22-
sb.append(chars[i]);
23-
i++;
24-
}
25-
if (i == chars.length) {
26-
if (sb.toString().length() != 0) {
27-
filteredStr.add(sb.toString());
28-
}
13+
Deque<String> stack = new LinkedList<>();
14+
for (int i = 0; i < s.length(); i++) {
15+
if (s.charAt(i) == ' ') {
16+
continue;
2917
} else {
30-
if (sb.toString().length() != 0) {
31-
filteredStr.add(sb.toString());
32-
}
33-
if (chars[i] == '+' || chars[i] == '-' || chars[i] == '(' || chars[i] == ')') {
34-
filteredStr.add(String.valueOf(chars[i]));
35-
}
36-
i++;
37-
}
38-
}
39-
40-
for (String str : filteredStr) {
41-
System.out.print(str);
42-
}
43-
44-
Stack<String> stack1 = new Stack();
45-
Stack<String> stack2 = new Stack();
46-
for (int i = 0; i < filteredStr.size(); ) {
47-
while (i < filteredStr.size() && !filteredStr.get(i).equals(")")) {
48-
stack1.push(filteredStr.get(i));
49-
i++;
50-
}
51-
if (i != filteredStr.size()) {
52-
while (!stack1.isEmpty() && !stack1.peek().equals("(")) {
53-
stack2.push(stack1.pop());
54-
}
55-
stack1.pop();
56-
int exp = 0;
57-
while (!stack2.isEmpty()) {
58-
if (stack2.size() == 1) {
59-
stack1.push(stack2.pop());
60-
break;
18+
if (s.charAt(i) == '(' || s.charAt(i) == '+' || s.charAt(i) == '-') {
19+
stack.addLast(s.charAt(i) + "");
20+
} else if (Character.isDigit(s.charAt(i))) {
21+
int start = i;
22+
while (i < s.length() && Character.isDigit(s.charAt(i))) {
23+
i++;
24+
}
25+
stack.addLast(s.substring(start, i));
26+
i--;
27+
} else if (s.charAt(i) == ')') {
28+
int result = 0;
29+
while (!stack.isEmpty() && !stack.peekLast().equals("(")) {
30+
String numStr = stack.pollLast();
31+
int numInt = Integer.parseInt(numStr);
32+
if (!stack.isEmpty() && (stack.peekLast().equals("-") || stack.peekLast().equals("+"))) {
33+
String operator = stack.pollLast();
34+
if (operator.equals("+")) {
35+
result += numInt;
36+
} else if (operator.equals("-")) {
37+
result -= numInt;
38+
}
39+
} else {
40+
result += numInt;
41+
if (!stack.isEmpty() && stack.peekLast().equals("(")) {
42+
stack.pollLast();
43+
break;
44+
}
45+
}
6146
}
62-
int operand1 = Integer.parseInt(stack2.pop());
63-
String operator = stack2.pop();
64-
int operand2 = Integer.parseInt(stack2.pop());
65-
if (operator.equals("+")) {
66-
exp = operand1 + operand2;
67-
} else if (operator.equals("-")) {
68-
exp = operand1 - operand2;
47+
if (!stack.isEmpty() && stack.peekLast().equals("(")) {
48+
stack.pollLast();
6949
}
70-
stack2.push(String.valueOf(exp));
50+
stack.addLast(result + "");
7151
}
72-
i++;
7352
}
7453
}
75-
76-
if (stack1.size() == 1) {
77-
return Integer.parseInt(stack1.pop());
78-
}
79-
80-
while (!stack1.isEmpty()) {
81-
stack2.push(stack1.pop());
82-
}
83-
while (!stack2.isEmpty()) {
84-
if (stack2.size() == 1) {
85-
stack1.push(stack2.pop());
86-
break;
87-
}
88-
int exp = 0;
89-
int operand1 = Integer.parseInt(stack2.pop());
90-
String operator = stack2.pop();
91-
int operand2 = Integer.parseInt(stack2.pop());
92-
if (operator.equals("+")) {
93-
exp = operand1 + operand2;
94-
} else if (operator.equals("-")) {
95-
exp = operand1 - operand2;
54+
int result = 0;
55+
while (!stack.isEmpty() && stack.peekLast() != "(") {
56+
String numStr = stack.pollLast();
57+
int numInt = Integer.parseInt(numStr);
58+
if (!stack.isEmpty()) {
59+
String operator = stack.pollLast();
60+
if (operator.equals("+")) {
61+
result += numInt;
62+
} else if (operator.equals("-")) {
63+
result -= numInt;
64+
}
65+
} else {
66+
result += numInt;
9667
}
97-
stack2.push(String.valueOf(exp));
9868
}
99-
return Integer.parseInt(stack1.pop());
69+
return !stack.isEmpty() ? Integer.parseInt(stack.peekLast()) + result : result;
10070
}
71+
10172
}
10273

10374
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._224;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _224Test {
10+
private static _224.Solution1 solution1;
11+
private static int expected;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _224.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
String s = "1 + 1";
21+
expected = 2;
22+
assertEquals(expected, solution1.calculate(s));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
String s = " 2-1 + 2 ";
28+
expected = 3;
29+
assertEquals(expected, solution1.calculate(s));
30+
}
31+
32+
@Test
33+
public void test3() {
34+
String s = "(1+(4+5+2)-3)+(6+8)";
35+
expected = 23;
36+
assertEquals(expected, solution1.calculate(s));
37+
}
38+
39+
@Test
40+
public void test4() {
41+
String s = "1-(-2)";
42+
expected = 3;
43+
assertEquals(expected, solution1.calculate(s));
44+
}
45+
46+
}

0 commit comments

Comments
 (0)