Open Tcdian opened 4 years ago
/**
* @param {number} n
* @param {number} k
* @return {number[][]}
*/
var combine = function(n, k) {
const result = [];
const combination = [];
backtracking(1);
return result;
function backtracking(num) {
if (combination.length === k) {
result.push([...combination]);
return;
}
if (num > n) {
return;
}
combination.push(num);
backtracking(num + 1);
combination.pop();
backtracking(num + 1);
}
};
function combine(n: number, k: number): number[][] {
const result: number[][] = [];
const combination: number[] = [];
backtracking(1);
return result;
function backtracking(num: number) {
if (combination.length === k) {
result.push([...combination]);
return;
}
if (num > n) {
return;
}
combination.push(num);
backtracking(num + 1);
combination.pop();
backtracking(num + 1);
}
};
77. Combinations
给定两个整数
n
和k
,返回1 ... n
中所有可能的k
个数的组合。Example 1
Example 2
Constraints
1 <= n <= 20
1 <= k <= n