Skip to content

Commit c238f82

Browse files
committed
Runtime: 260 ms (Top 25.49%) | Memory: 50.2 MB (Top 45.10%)
1 parent 3ef9eae commit c238f82

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1+
// Runtime: 260 ms (Top 25.49%) | Memory: 50.2 MB (Top 45.10%)
12
var addOperators = function(num, target) {
23
let res = [];
3-
4+
45
let i = 0;
5-
6+
67
const helper = (idx, sum, prev, path) => {
78
if(idx >= num.length) {
89
if(sum === target) {
910
res.push(path);
1011
}
1112
return null;
1213
}
13-
14+
1415
for(let j = idx; j < num.length; j++) {
15-
16+
1617
if(j !== idx && num[idx] === "0")
1718
break;
18-
19+
1920
let n = Number(num.slice(idx, j+1));
20-
21+
2122
if(idx === 0) {
2223
helper(j + 1, sum + n, sum + n, path + n);
2324
} else {
2425
helper(j + 1, sum + n, n, path + "+" + n);
25-
26+
2627
helper(j + 1, sum - n, 0 - n, path + "-" + n);
27-
28+
2829
helper(j + 1, sum - prev + (prev * n), prev * n, path + "*" + n);
2930
}
3031
}
31-
32+
3233
}
33-
34+
3435
helper(i, 0, 0, "");
35-
36+
3637
return res;
37-
};
38+
};

0 commit comments

Comments
 (0)