Solution1:我的答案
DFS+backtracking,时间复杂度
,空间复杂度
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int> > res;
vector<int> temp;
int start = 1;
my_combinate(res, temp, n, k, start);
return res;
}
void my_combinate(vector<vector<int> >& res, vector<int>& temp,
int n, int k, int start) {
if (temp.size() == k) {
if (n == 0)
res.push_back(temp);
return;
} else {
for (int i = start; i <= 9; i++) {
temp.push_back(i);
my_combinate(res, temp, n - i, k, i + 1);
temp.pop_back();
}
}
}
};