Skip to content

Commit 575ca96

Browse files
just4oncegitbook-bot
authored andcommitted
GitBook: [master] one page modified
1 parent 1389972 commit 575ca96

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

leetcode/divide-and-conquer/241-different-ways-to-add-parentheses.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,34 @@ Explanation:
3434
2. From the return list, we can add the corresponding value based on operators to our list
3535
3. Time complexity O\(3^n\)
3636
4. Space complexity O\(3^n\)
37-
2.
37+
2. Divide and Conquer \(Cached\)
38+
1.
39+
3. asd
40+
3841
## Solution
3942

4043
```java
41-
44+
class Solution {
45+
public List<Integer> diffWaysToCompute(String input) {
46+
List<Integer> res = new ArrayList<>();
47+
for (int i = 0; i < input.length(); i++) {
48+
char c = input.charAt(i);
49+
if (c == '+' || c == '-' || c == '*') {
50+
List<Integer> left = diffWaysToCompute(input.substring(0, i));
51+
List<Integer> right = diffWaysToCompute(input.substring(i + 1, input.length()));
52+
for (int n1 : left) {
53+
for (int n2 : right) {
54+
if (c == '+') res.add(n1 + n2);
55+
else if (c == '-') res.add(n1 - n2);
56+
else res.add(n1 * n2);
57+
}
58+
}
59+
}
60+
}
61+
if (res.size() == 0) res.add(Integer.parseInt(input));
62+
return res;
63+
}
64+
}
4265
```
4366

4467
## Additional {#additional}

0 commit comments

Comments
 (0)