Repository navigation
Expand file tree
/
Copy pathCombinationSumII.cpp
More file actions
51 lines (41 loc) · 2.14 KB
/
Copy pathCombinationSumII.cpp
File metadata and controls
51 lines (41 loc) · 2.14 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
class Solution {
public:
// Main function to find all unique combinations that sum up to the target
vector<vector<int>> combinationSum2(vector<int>& cand, int target) {
// Vector to store the final unique combinations
vector<vector<int>> ans;
// Sort the candidates to help with pruning and skipping duplicates
sort(cand.begin(), cand.end());
// Temporary vector to store a single combination
vector<int> temp;
// Call the backtracking function to generate combinations
backtrackCombination(ans, temp, cand, 0, target);
// Return the list of all valid unique combinations
return ans;
}
// Backtracking function to explore combinations recursively
void backtrackCombination(vector<vector<int>>& ans, vector<int>& temp, const vector<int>& cand, int index, int target) {
// Base case: if the target becomes 0, we have found a valid combination
if (target == 0) {
ans.push_back(temp); // Add the current combination to the answer
return;
}
// Iterate through the candidates starting from the current index
for (int i = index; i < cand.size(); i++) {
// If the current candidate exceeds the remaining target, stop further exploration
if (cand[i] > target) {
return; // No need to backtrack further as all candidates are sorted
}
// Skip duplicates: If the current candidate is the same as the previous one, skip it
if (i > index && cand[i] == cand[i - 1]) {
continue; // Skip this candidate to avoid duplicate combinations
}
// Include the current candidate in the combination
temp.push_back(cand[i]);
// Recurse with the updated target and next index (i + 1 to avoid reusing the same element)
backtrackCombination(ans, temp, cand, i + 1, target - cand[i]);
// Backtrack by removing the last element to explore other possibilities
temp.pop_back();
}
}
};