File tree 3 files changed +50
-6
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder
3 files changed +50
-6
lines changed Original file line number Diff line number Diff line change @@ -1220,7 +1220,7 @@ _If you like this project, please leave me a star._ ★
1220
1220
| 227 | [Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_227.java) | | Medium | String
1221
1221
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_226.java) | | Easy | DFS, recursion
1222
1222
| 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
1224
1224
| 223 | [Rectangle Area](https://leetcode.com/problems/rectangle-area/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_223.java) | | Easy |
1225
1225
| 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
1226
1226
| 221 | [Maximal Square](https://leetcode.com/problems/maximal-square/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_221.java) | | Medium | Recursion
Original file line number Diff line number Diff line change @@ -68,7 +68,45 @@ public int calculate(String s) {
68
68
}
69
69
return !stack .isEmpty () ? Integer .parseInt (stack .peekLast ()) + result : result ;
70
70
}
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 ;
71
81
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
+ }
72
110
}
73
111
74
112
}
Original file line number Diff line number Diff line change 1
1
package com .fishercoder ;
2
2
3
3
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 ;
6
6
7
- import static org .junit .Assert .assertEquals ;
7
+ import static org .junit .jupiter . api . Assertions .assertEquals ;
8
8
9
9
public class _224Test {
10
10
private static _224 .Solution1 solution1 ;
11
+ private static _224 .Solution2 solution2 ;
11
12
private static int expected ;
12
13
13
- @ BeforeClass
14
- public static void setup () {
14
+ @ BeforeEach
15
+ public void setup () {
15
16
solution1 = new _224 .Solution1 ();
17
+ solution2 = new _224 .Solution2 ();
16
18
}
17
19
18
20
@ Test
19
21
public void test1 () {
20
22
String s = "1 + 1" ;
21
23
expected = 2 ;
22
24
assertEquals (expected , solution1 .calculate (s ));
25
+ assertEquals (expected , solution2 .calculate (s ));
23
26
}
24
27
25
28
@ Test
26
29
public void test2 () {
27
30
String s = " 2-1 + 2 " ;
28
31
expected = 3 ;
29
32
assertEquals (expected , solution1 .calculate (s ));
33
+ assertEquals (expected , solution2 .calculate (s ));
30
34
}
31
35
32
36
@ Test
33
37
public void test3 () {
34
38
String s = "(1+(4+5+2)-3)+(6+8)" ;
35
39
expected = 23 ;
36
40
assertEquals (expected , solution1 .calculate (s ));
41
+ assertEquals (expected , solution2 .calculate (s ));
37
42
}
38
43
39
44
@ Test
40
45
public void test4 () {
41
46
String s = "1-(-2)" ;
42
47
expected = 3 ;
43
48
assertEquals (expected , solution1 .calculate (s ));
49
+ assertEquals (expected , solution2 .calculate (s ));
44
50
}
45
51
46
52
}
You can’t perform that action at this time.
0 commit comments