Skip to content

Commit 6df662c

Browse files
just4oncegitbook-bot
authored andcommitted
GitBook: [master] one page modified
1 parent b8ba149 commit 6df662c

File tree

1 file changed

+105
-4
lines changed

1 file changed

+105
-4
lines changed

leetcode/divide-and-conquer/282-expression-add-operators.md

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,116 @@ Output: []
4444
## Thought Process {#thought-process}
4545

4646
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
47+
1. We can divide the number at each position and check if there is way to get target using different operations
48+
2. At each position \(after getting the value\), we recursively look at these three operations
49+
3. If we only have +-, the computation is easier, we can move forward without worry about the previous value being used in multiply
50+
4. Now, we need a separate variable to hold the previous value and reverse the result by subtracting previous value and then compute with right order
51+
5. We create a separate function recur\(chars, index, value, pre, stringbuilder, target, result\) where index points to the current character and value is the current evaluated result, pre is previous number, stringbuilder holds the
52+
6. Time complexity O\(n\*3^n\), where n^2 comes from n loops and n character in stringBuilder.toString\(\), and 3^n from 3 operators and looping for each digit
53+
7. Space complexity O\(n^2\*3^n\), where n^2 comes from ways to break operands
54+
8. Other suggests:
55+
9. T\(n\) = 3 _T\(n-1\) + 3_ T\(n-2\) + 3 _T\(n-3\) + ... + 3_ T\(1\);
56+
10. T\(n-1\) = 3 _T\(n-2\) + 3_ T\(n-3\) + ... 3 \* T\(1\);
57+
11. Thus T\(n\) = 4T\(n-1\);
58+
2. Divide and Conquer \(with path array\)
59+
1. Instead of using string builder, we use a char array to save the path that get to the current point, if the value == target, we can save the path
60+
2. Moreover, we can also postpone the operation until we meet the next operator, this way we don't have to do any retracting
5161

5262
## Solution
5363

5464
```java
65+
class Solution {
66+
public List<String> addOperators(String num, int target) {
67+
// If the question is simply +-, the result is easy
68+
// because we have *, we need to track previous number and
69+
// reverse when we use multiply
70+
List<String> res = new ArrayList<>();
71+
if (num.length() == 0) return res;
72+
char[] chars = num.toCharArray();
73+
long value = 0;
74+
StringBuilder sb = new StringBuilder();
75+
for (int i = 0; i < chars.length; i++) {
76+
// try each reak point
77+
value = value * 10 + chars[i] - '0';
78+
sb.setLength(0);
79+
sb.append(value);
80+
recur(chars, i + 1, value, value, sb, target, res);
81+
if (chars[0] == '0') break;
82+
}
83+
return res;
84+
}
85+
86+
private void recur(char[] chars, int index, long value, long pre, StringBuilder sb, int target, List<String> res) {
87+
if (index == chars.length) {
88+
if (value == target) res.add(sb.toString());
89+
} else {
90+
// initialize
91+
long cur = 0;
92+
// save the length for backtrack
93+
int len = sb.length();
94+
for (int i = index; i < chars.length; i++) {
95+
cur = cur * 10 + chars[i] - '0';
96+
// multiply
97+
recur(chars, i + 1, value - pre + pre * cur, pre * cur, sb.append("*").append(cur), target, res);
98+
sb.setLength(len);
99+
// add
100+
recur(chars, i + 1, value + cur, cur, sb.append("+").append(cur), target, res);
101+
sb.setLength(len);
102+
// subtract
103+
recur(chars, i + 1, value - cur, -cur, sb.append("-").append(cur), target, res);
104+
sb.setLength(len);
105+
if (chars[index] == '0') break;
106+
}
107+
}
108+
}
109+
}
110+
```
55111

112+
```java
113+
class Solution {
114+
public List<String> addOperators(String num, int target) {
115+
// If the question is simply +-, the result is easy
116+
// because we have *, we need to track previous number and
117+
// reverse when we use multiply
118+
List<String> res = new ArrayList<>();
119+
if (num.length() == 0) return res;
120+
char[] chars = num.toCharArray();
121+
int n = chars.length;
122+
char[] path = new char[2 * n - 1];
123+
long value = 0;
124+
for (int i = 0; i < chars.length; i++) {
125+
// try each reak point
126+
value = value * 10 + chars[i] - '0';
127+
path[i] = chars[i];
128+
recur(chars, i + 1, 0, value, path, i + 1, target, res);
129+
if (value == 0) break;
130+
}
131+
return res;
132+
}
133+
134+
private void recur(char[] chars, int index, long left, long pre, char[] path, int len, int target, List<String> res) {
135+
if (index == chars.length) {
136+
if (left + pre == target) res.add(new String(path, 0, len));
137+
} else {
138+
// initialize
139+
long cur = 0;
140+
int j = len + 1;
141+
for (int i = index; i < chars.length; i++) {
142+
cur = cur * 10 + chars[i] - '0';
143+
path[j++] = chars[i];
144+
// multiply
145+
path[len] = '*';
146+
recur(chars, i + 1, left, pre * cur, path, j, target, res);
147+
// add
148+
path[len] = '+';
149+
recur(chars, i + 1, left + pre, cur, path, j, target, res);
150+
// subtract
151+
path[len] = '-';
152+
recur(chars, i + 1, left + pre, -cur, path, j, target, res);
153+
}
154+
}
155+
}
156+
}
56157
```
57158

58159
## Additional {#additional}

0 commit comments

Comments
 (0)