Skip to content

Commit b8ba149

Browse files
just4oncegitbook-bot
authored andcommitted
GitBook: [master] 3 pages modified
1 parent 575ca96 commit b8ba149

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
* [215-kth-largest-element-in-an-array](leetcode/divide-and-conquer/215-kth-largest-element-in-an-array.md)
6060
* [218-the-skyline-problem](leetcode/divide-and-conquer/218-the-skyline-problem.md)
6161
* [241-different-ways-to-add-parentheses](leetcode/divide-and-conquer/241-different-ways-to-add-parentheses.md)
62+
* [282-expression-add-operators](leetcode/divide-and-conquer/282-expression-add-operators.md)
6263
* [hash-table](leetcode/hash-table/README.md)
6364
* [003-longest-substring-without-repeating-characters](leetcode/hash-table/003-longest-substring-without-repeating-characters.md)
6465
* [030-substring-with-concatenation-of-all-words](leetcode/hash-table/030-substring-with-concatenation-of-all-words.md)

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ Explanation:
3535
3. Time complexity O\(3^n\)
3636
4. Space complexity O\(3^n\)
3737
2. Divide and Conquer \(Cached\)
38-
1.
38+
1. Avoid repetitive calculation
39+
2. Time complexity O\(?\)
40+
3. Space complexity o\(?\)
3941
3. asd
4042

4143
## Solution
@@ -64,5 +66,38 @@ class Solution {
6466
}
6567
```
6668

69+
```java
70+
class Solution {
71+
72+
public List<Integer> diffWaysToCompute(String input) {
73+
// Use cache to avoid repeative calculaiton
74+
Map<String, List<Integer>> cache = new HashMap<>();
75+
return search(input, cache);
76+
}
77+
78+
private List<Integer> search(String input, Map<String, List<Integer>> cache) {
79+
if (cache.containsKey(input)) return cache.get(input);
80+
List<Integer> res = new ArrayList<>();
81+
for (int i = 0; i < input.length(); i++) {
82+
char c = input.charAt(i);
83+
if (c == '+' || c == '-' || c == '*') {
84+
List<Integer> left = diffWaysToCompute(input.substring(0, i));
85+
List<Integer> right = diffWaysToCompute(input.substring(i + 1, input.length()));
86+
for (int n1 : left) {
87+
for (int n2 : right) {
88+
if (c == '+') res.add(n1 + n2);
89+
else if (c == '-') res.add(n1 - n2);
90+
else res.add(n1 * n2);
91+
}
92+
}
93+
}
94+
}
95+
if (res.size() == 0) res.add(Integer.parseInt(input));
96+
cache.put(input, res);
97+
return res;
98+
}
99+
}
100+
```
101+
67102
## Additional {#additional}
68103

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 282-expression-add-operators
2+
3+
## Question {#question}
4+
5+
Given a string that contains only digits `0-9` and a target value, return all possibilities to add **binary** operators \(not unary\) `+`, `-`, or `*`between the digits so they evaluate to the target value.
6+
7+
**Example 1:**
8+
9+
```text
10+
Input: num = "123", target = 6
11+
Output: ["1+2+3", "1*2*3"]
12+
```
13+
14+
**Example 2:**
15+
16+
```text
17+
Input: num = "232", target = 8
18+
Output: ["2*3+2", "2+3*2"]
19+
```
20+
21+
**Example 3:**
22+
23+
```text
24+
Input: num = "105", target = 5
25+
Output: ["1*0+5","10-5"]
26+
```
27+
28+
**Example 4:**
29+
30+
```text
31+
Input: num = "00", target = 0
32+
Output: ["0+0", "0-0", "0*0"]
33+
```
34+
35+
**Example 5:**
36+
37+
```text
38+
Input: num = "3456237490", target = 9191
39+
Output: []
40+
```
41+
42+
43+
44+
## Thought Process {#thought-process}
45+
46+
1. Divide and Conquer
47+
1. Similar to 241, we can divide the number at each position and check if there is way to get target using different operation
48+
2. Time complexity O\(?\)
49+
3. Space complexity O\(?\)
50+
2. asd
51+
52+
## Solution
53+
54+
```java
55+
56+
```
57+
58+
## Additional {#additional}
59+

0 commit comments

Comments
 (0)