Skip to content

Commit 7b5bd29

Browse files
refactor 241
1 parent cc19e0d commit 7b5bd29

File tree

1 file changed

+41
-38
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+41
-38
lines changed

src/main/java/com/fishercoder/solutions/_241.java

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,52 @@
2929
3030
*/
3131
public class _241 {
32-
/**Time: O(n * 4^n / n^(3/2)) ~= n * Catalan numbers = n * (C(2n, n) - C(2n, n - 1)),
33-
due to the size of the results is Catalan numbers,
34-
and every way of evaluation is the length of the string,
35-
so the time complexity is at most n * Catalan numbers.
36-
Space: O(n * 4^n / n^(3/2)), the cache size of lookup is at most n * Catalan numbers.*/
32+
public static class Solution1 {
33+
/**Time: O(n * 4^n / n^(3/2)) ~= n * Catalan numbers = n * (C(2n, n) - C(2n, n - 1)),
34+
due to the size of the results is Catalan numbers,
35+
and every way of evaluation is the length of the string,
36+
so the time complexity is at most n * Catalan numbers.
37+
Space: O(n * 4^n / n^(3/2)), the cache size of lookup is at most n * Catalan numbers.*/
3738

38-
/**Credit: https://discuss.leetcode.com/topic/19901/a-recursive-java-solution-284-ms*/
39-
public List<Integer> diffWaysToCompute(String input) {
40-
List<Integer> answer = new LinkedList<>();
41-
int len = input.length();
42-
for (int i = 0; i < len; i++) {
43-
if (input.charAt(i) == '+'
44-
|| input.charAt(i) == '-'
45-
|| input.charAt(i) == '*') {
46-
String part1 = input.substring(0, i);
47-
String part2 = input.substring(i + 1);
48-
List<Integer> answer1 = diffWaysToCompute(part1);
49-
List<Integer> answer2 = diffWaysToCompute(part2);
50-
for (int a1 : answer1) {
51-
for (int a2 : answer2) {
52-
int result = 0;
53-
switch (input.charAt(i)) {
54-
case '+':
55-
result = a1 + a2;
56-
break;
57-
case '-':
58-
result = a1 - a2;
59-
break;
60-
case '*':
61-
result = a1 * a2;
62-
break;
63-
default:
64-
break;
39+
/**
40+
* Credit: https://discuss.leetcode.com/topic/19901/a-recursive-java-solution-284-ms
41+
*/
42+
public List<Integer> diffWaysToCompute(String input) {
43+
List<Integer> answer = new LinkedList<>();
44+
int len = input.length();
45+
for (int i = 0; i < len; i++) {
46+
if (input.charAt(i) == '+'
47+
|| input.charAt(i) == '-'
48+
|| input.charAt(i) == '*') {
49+
String part1 = input.substring(0, i);
50+
String part2 = input.substring(i + 1);
51+
List<Integer> answer1 = diffWaysToCompute(part1);
52+
List<Integer> answer2 = diffWaysToCompute(part2);
53+
for (int a1 : answer1) {
54+
for (int a2 : answer2) {
55+
int result = 0;
56+
switch (input.charAt(i)) {
57+
case '+':
58+
result = a1 + a2;
59+
break;
60+
case '-':
61+
result = a1 - a2;
62+
break;
63+
case '*':
64+
result = a1 * a2;
65+
break;
66+
default:
67+
break;
68+
}
69+
answer.add(result);
6570
}
66-
answer.add(result);
6771
}
6872
}
6973
}
74+
if (answer.size() == 0) {
75+
answer.add(Integer.valueOf(input));
76+
}
77+
return answer;
7078
}
71-
if (answer.size() == 0) {
72-
answer.add(Integer.valueOf(input));
73-
}
74-
return answer;
7579
}
76-
7780
}

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