forked from ManishGupta1908/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres.ts
26 lines (22 loc) · 698 Bytes
/
res.ts
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
function iterateNums(nums: number[], k: number): number[][] {
if (k === 0) {
return [];
}
const result: number[][] = [];
nums.forEach((num, index) => {
const newNums = nums.slice(index+1);
const newResult = iterateNums([...newNums], k - 1);
if (newResult.length) {
newResult.forEach(item => {
result.push([num, ...item]);
});
} else {
result.push([num]);
}
});
return result.filter(item => item.length === k);
}
function combine(n: number, k: number): number[][] {
const nums = (new Array(n).fill(0)).map((_, index) => index + 1);
return iterateNums(nums, k);
};