无非是多加了一些条件,方便剪枝
class Solution {
public:
int n;
int k;
int sum = 0;
vector<int> tmp;
vector<vector<int>> result;
void dfs(int x){
cout << this->sum << endl;
if(this->tmp.size() >= k){
if(this->sum == this->n){
this->result.push_back(this->tmp);
}
return;
}else if(this->sum < this->n){
for(int i = x; i <= min(this->n - this->sum, 9); i ++){
this->tmp.push_back(i);
this->sum += i;
dfs(i + 1);
this->sum -= i;
this->tmp.pop_back();
}
}
}
vector<vector<int>> combinationSum3(int k, int n) {
this->k = k;
this->n = n;
dfs(1);
return this->result;
}
};