/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*
输入: k = 3, n = 7
输出: [[1,2,4]]
输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
*/
var combinationSum3 = function(k, n) {
var result = [];
var path = [];
var startIndex = 1;
function getSum(total, num) {
return total + num;
}
function backtracking(index){
var num = path.reduce(getSum,0);
if(path.length>=k || num>=n){
if(num===n&&path.length===k){
result.push([...path]);
}
return
}
for(var i=index;i<(n-(k-path.length)+1)&&i<=9;i++){
path.push(i);
backtracking(i+1);
path.pop();
}
}
backtracking(startIndex);
// return result;
console.log(result);
};
combinationSum3(2,9);
console