forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0039.cpp
More file actions
25 lines (23 loc) · 735 Bytes
/
0039.cpp
File metadata and controls
25 lines (23 loc) · 735 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
25
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> path;
sort(candidates.begin(), candidates.end());
dfs(candidates, target, 0, path, ans);
return ans;
}
private:
void dfs(vector<int>& candidates, int target, int s, vector<int>& path, vector<vector<int>>& ans) {
if (target < 0) return;
if (target == 0) {
ans.push_back(path);
return;
}
for (int i = s; i < candidates.size(); i++) {
path.push_back(candidates[i]);
dfs(candidates, target - candidates[i], i, path, ans);
path.pop_back();
}
}
};