-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinations.js
43 lines (43 loc) · 914 Bytes
/
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
/**
* @param {number} n
* @param {number} k
* @return {number[][]}
*/
let res = [];
let path = [];
var combine = function(n, k) {
let res = [];
let path = [];
backtracking(n,k,1);
return res;
};
const backtracking = (n,k,startIndex) =>{
if(path.length===k){
res.push([...path])
return
}
for(i = startIndex;i<=n;++i){
path.push(i)
backtracking(n,k,i+1)
path.pop(i)
}
}
// let result = []
// let path = []
// var combine = function(n, k) {
// result = []
// combineHelper(n, k, 1)
// return result
// };
// const combineHelper = (n, k, startIndex) => {
// if (path.length === k) {
// result.push([...path])
// return
// }
// for (let i = startIndex; i <= n - (k - path.length) + 1; ++i) {
// path.push(i)
// combineHelper(n, k, i + 1)
// path.pop()
// }
// }
combine(4,2);