class Solution {
public:
vector<vector<int>> res;
vector<int> path;
int sum = 0;
void dfs(int start, int k, int n) {
if (path.size() > k || sum > n) return;
if (path.size() == k && sum == n) {
res.push_back(path);
return;
}
for (int i = start; i <= 9; i ++) {
path.push_back(i);
sum += i;
dfs(i + 1, k, n);
path.pop_back();
sum -= i;
}
}
vector<vector<int>> combinationSum3(int k, int n) {
dfs(1, k, n);
return res;
}
};
标签:216,return,int,res,sum,path,III,LeetCode
From: https://www.cnblogs.com/hjy94wo/p/16655080.html