-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombinations.js
67 lines (53 loc) · 1.18 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Combinations
*
* Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
*
* Example:
*
* Input: n = 4, k = 2
* Output:
* [
* [2,4],
* [3,4],
* [2,3],
* [1,2],
* [1,3],
* [1,4],
* ]
*/
const combinations = (n, k) => {
return backtracking(n, k);
};
const backtracking = (num, count, start = 1, list = [], result = []) => {
if (list.length > count) {
return result;
}
if (list.length === count) {
return result.push(list.slice());
}
for (let i = start; i <= num; i++) {
list.push(i);
backtracking(num, count, i + 1, list, result);
list.pop();
}
return result;
};
// function combinations (m, n) {
// const nums = [0, 1, 2, 3, 4];
// const result = [];
// backtracking(nums, '', 0, 4);
// function backtracking(nums, str, start, len) {
// if (str.length === 3) {
// const t = str.split('').sort().join('');
// if (result.indexOf(t) === -1) {
// result.push(t);
// }
// }
// for (let i = start; i <= len; i++) {
// backtracking(nums, str + nums[i], start + 1, len);
// }
// }
// return result;
// }
console.log(combinations(4, 2));