Skip to content

Commit 38b14ed

Browse files
committedJun 5, 2024
add a solution for 224
1 parent 4bdc583 commit 38b14ed

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ _If you like this project, please leave me a star._ ★
12201220
| 227 | [Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_227.java) | | Medium | String
12211221
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_226.java) | | Easy | DFS, recursion
12221222
| 225 | [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_225.java) | | Easy | Stack, Queue
1223-
| 224 | [Basic Calculator](https://leetcode.com/problems/basic-calculator/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_224.java) | | Hard |
1223+
| 224 | [Basic Calculator](https://leetcode.com/problems/basic-calculator/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_224.java) | | Hard | Recursion
12241224
| 223 | [Rectangle Area](https://leetcode.com/problems/rectangle-area/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_223.java) | | Easy |
12251225
| 222 | [Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_222.java) | | Medium | Recursion
12261226
| 221 | [Maximal Square](https://leetcode.com/problems/maximal-square/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_221.java) | | Medium | Recursion

‎src/main/java/com/fishercoder/solutions/_224.java

+38
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,45 @@ public int calculate(String s) {
6868
}
6969
return !stack.isEmpty() ? Integer.parseInt(stack.peekLast()) + result : result;
7070
}
71+
}
72+
73+
public static class Solution2 {
74+
/**
75+
* Simple and clean recursion solution, credit: https://leetcode.com/problems/basic-calculator/solutions/2344042/java-2ms-100-recursion-easy-to-understand/
76+
* Key points:
77+
* 1. it uses a global variable called index to control which char to iterate on;
78+
* 2. it passes the entire string s into recursive functions.
79+
*/
80+
int index;
7181

82+
public int calculate(String s) {
83+
index = 0;
84+
return cal(s);
85+
}
86+
87+
private int cal(String s) {
88+
int result = 0;
89+
int num = 0;
90+
int sign = 1;
91+
while (index < s.length()) {
92+
char c = s.charAt(index++);
93+
if (c >= '0' && c <= '9') {
94+
num = num * 10 + c - '0';
95+
} else if (c == '(') {
96+
//this is the beginning of a new sub-problem, we let recursion do its job
97+
num = cal(s);
98+
} else if (c == ')') {
99+
//this is the end of a problem/sub-problem, so we return
100+
return result + sign * num;
101+
} else if (c == '+' || c == '-') {
102+
//now we know we finished reading one number and a new number has begun
103+
result += sign * num;
104+
num = 0;
105+
sign = c == '-' ? -1 : 1;
106+
}
107+
}
108+
return result + sign * num;
109+
}
72110
}
73111

74112
}
+11-5
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._224;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _224Test {
1010
private static _224.Solution1 solution1;
11+
private static _224.Solution2 solution2;
1112
private static int expected;
1213

13-
@BeforeClass
14-
public static void setup() {
14+
@BeforeEach
15+
public void setup() {
1516
solution1 = new _224.Solution1();
17+
solution2 = new _224.Solution2();
1618
}
1719

1820
@Test
1921
public void test1() {
2022
String s = "1 + 1";
2123
expected = 2;
2224
assertEquals(expected, solution1.calculate(s));
25+
assertEquals(expected, solution2.calculate(s));
2326
}
2427

2528
@Test
2629
public void test2() {
2730
String s = " 2-1 + 2 ";
2831
expected = 3;
2932
assertEquals(expected, solution1.calculate(s));
33+
assertEquals(expected, solution2.calculate(s));
3034
}
3135

3236
@Test
3337
public void test3() {
3438
String s = "(1+(4+5+2)-3)+(6+8)";
3539
expected = 23;
3640
assertEquals(expected, solution1.calculate(s));
41+
assertEquals(expected, solution2.calculate(s));
3742
}
3843

3944
@Test
4045
public void test4() {
4146
String s = "1-(-2)";
4247
expected = 3;
4348
assertEquals(expected, solution1.calculate(s));
49+
assertEquals(expected, solution2.calculate(s));
4450
}
4551

4652
}

0 commit comments

Comments
 (0)