-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path216.cpp
More file actions
24 lines (24 loc) · 696 Bytes
/
216.cpp
File metadata and controls
24 lines (24 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<vector<int>> res;
void backtracking(int k, vector<int>& path, int currStart, int currSum, int n) {
if (currSum == n && path.size() == k) {
res.push_back(path);
return;
}
if (path.size() > k) return;
if (currSum > n) return;
else {
for (int i = currStart; i <= 9; i++) {
path.push_back(i);
backtracking(k, path, i + 1, currSum + i, n);
path.pop_back();
}
}
}
vector<vector<int>> combinationSum3(int k, int n) {
vector<int> path;
backtracking(k, path, 1, 0, n);
return res;
}
};