|
29 | 29 |
|
30 | 30 | */
|
31 | 31 | 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.*/ |
37 | 38 |
|
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); |
65 | 70 | }
|
66 |
| - answer.add(result); |
67 | 71 | }
|
68 | 72 | }
|
69 | 73 | }
|
| 74 | + if (answer.size() == 0) { |
| 75 | + answer.add(Integer.valueOf(input)); |
| 76 | + } |
| 77 | + return answer; |
70 | 78 | }
|
71 |
| - if (answer.size() == 0) { |
72 |
| - answer.add(Integer.valueOf(input)); |
73 |
| - } |
74 |
| - return answer; |
75 | 79 | }
|
76 |
| - |
77 | 80 | }
|
0 commit comments