216. Combination Sum III
# Medium
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<int> subComb;
vector<vector<int>> comb;
helper(comb, subComb, k, n, 1);
return comb;
}
void helper(vector<vector<int>> &comb, vector<int> &subComb, int k, int n, int i) {
// stop condition
if(subComb.size() == k) {
if(n == 0) comb.push_back(subComb);
return;
}
// regular case
for(int j = i; j <= 9; j ++) {
subComb.push_back(j);
helper(comb, subComb, k, n-j, j+1);
subComb.pop_back();
}
}
};
Still confuse whether vector<int>subComb should use reference or original list? But using reference makes this program faster, beat 100%.
Last updated
Was this helpful?