题目链接:https://leetcode-cn.com/problems/combination-sum-iii/
题目如下:
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
backtracking(n,k,1,0);
return result;
}
void backtracking(int n,int k,int startIndex,int sum){
if(sum>n) return ;
if(path.size()==k){
if(sum==n) result.push_back(path);
return ;
}
for(int i=startIndex;i<=9;i++){//题目中,要求数在1-9之间
sum+=i;
path.push_back(i);
backtracking(n,k,i+1,sum);
sum-=i;
path.pop_back();
}
}
private:
vector<vector<int>> result;
vector<int> path;
};