Skip to content

Commit 9955d3d

Browse files
authored
Create combinations.js
Create combinations.js
1 parent 1f28db4 commit 9955d3d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

JavaScript/combinations.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} k
4+
* @return {number[][]}
5+
*/
6+
7+
var combine = function(n, k) {
8+
if (n < k || k < 1) return [];
9+
10+
var res = [];
11+
12+
helper(res, [], 0, n, k);
13+
14+
return res;
15+
};
16+
17+
var helper = function (res, now, start, n, k) {
18+
if (k === 0) {
19+
res.push(Array.from(now));
20+
return;
21+
}
22+
23+
for (var i = start; i < n; i++) {
24+
now.push(i + 1)
25+
helper(res, now, i + 1, n, k - 1);
26+
now.pop();
27+
}
28+
29+
};

0 commit comments

Comments
 (0)