Skip to content

Commit 323d5a5

Browse files
authored
Create combination-sum.js
1 parent 373a3eb commit 323d5a5

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

combination-sum.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const combinationSum = (superset, start, sum) => {
2+
if (sum < 0) return;
3+
subset.length && sum === 0 && subsets.push(subset);
4+
for (let i = start; i < superset.length; i++) {
5+
subset = subset.concat(superset[i]);
6+
combinationSum(superset, i + 1, sum - superset[i]);
7+
subset = subset.slice(0, subset.length - 1);
8+
}
9+
}
10+
11+
let subsets = [];
12+
let subset = [];
13+
const superset = [1, 2, 3, 4, 1, 6, 7];
14+
combinationSum(superset, 0, 7);
15+
console.log(subsets);

0 commit comments

Comments
 (0)