-
Notifications
You must be signed in to change notification settings - Fork 0
/
77.combinations.js
49 lines (44 loc) · 903 Bytes
/
77.combinations.js
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
39
40
41
42
43
44
45
46
47
48
49
/*
* @lc app=leetcode id=77 lang=javascript
*
* [77] Combinations
*/
/**
* @param {number} n
* @param {number} k
* @return {number[][]}
*/
var combine = function(n, k) {
let res = [];
function helper(startInx, sub) {
if (sub.length === k) {
res.push(sub.slice());
return;
}
for (let i = startInx; i <= n; i++) {
if (sub.length >= 1 && sub[sub.length - 1] >= i) continue;
sub.push(i);
helper(startInx + 1, sub);
sub.pop();
}
}
helper(1, []);
return res;
};
var combine = function(n, k) {
let res = [];
function helper(startInx, sub) {
if (sub.length === k) {
res.push(sub.slice());
return;
}
for (let i = startInx; i <= n; i++) {
if (sub.length >= 1 && sub[sub.length - 1] >= i) continue;
sub.push(i);
helper(i + 1, sub);
sub.pop();
}
}
helper(1, []);
return res;
};