-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy path40_CombinationSumII.cpp
More file actions
55 lines (51 loc) · 1.46 KB
/
Copy path40_CombinationSumII.cpp
File metadata and controls
55 lines (51 loc) · 1.46 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* @Author: xuezaigds@gmail.com
* @Last Modified time: 2016-07-15 21:38:18
*/
class Solution {
/*
Classic backtracking problem.
One key point: for one specified number,
just scan the number larger than it to avoid duplicate combinations.
Besides, the current path need to be reset after dfs call in general.
That's saying, we need to do pop_back after push_back operator.
*/
private:
void dfs_search(vector<int> &candidates, int start, int target, vector<int> &path, vector<vector<int>> &ans){
if(target==0){
ans.push_back(path);
}
for(int i=start; i<candidates.size(); i++){
int num = candidates[i];
if(num > target){
return;
}
// Here skip the same `adjacent` element to avoid duplicated.
if(i > start && candidates[i] == candidates[i-1]){
continue;
}
path.push_back(candidates[i]);
dfs_search(candidates, i+1, target-num, path, ans);
path.pop_back();
}
}
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
if(candidates.size() == 0){
return {};
}
sort(candidates.begin(), candidates.end());
vector<int> path;
vector<vector<int>> ans;
dfs_search(candidates, 0, target, path, ans);
return ans;
}
};
/*
[]
1
[2, 5, 1, 4, 9]
11
[10, 1, 2, 7, 6, 1, 5]
8
*/