-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathExpression Add Operators.js
More file actions
38 lines (27 loc) · 894 Bytes
/
Copy pathExpression Add Operators.js
File metadata and controls
38 lines (27 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Runtime: 260 ms (Top 25.49%) | Memory: 50.2 MB (Top 45.10%)
var addOperators = function(num, target) {
let res = [];
let i = 0;
const helper = (idx, sum, prev, path) => {
if(idx >= num.length) {
if(sum === target) {
res.push(path);
}
return null;
}
for(let j = idx; j < num.length; j++) {
if(j !== idx && num[idx] === "0")
break;
let n = Number(num.slice(idx, j+1));
if(idx === 0) {
helper(j + 1, sum + n, sum + n, path + n);
} else {
helper(j + 1, sum + n, n, path + "+" + n);
helper(j + 1, sum - n, 0 - n, path + "-" + n);
helper(j + 1, sum - prev + (prev * n), prev * n, path + "*" + n);
}
}
}
helper(i, 0, 0, "");
return res;
};