---
# System prepended metadata

title: LC 40. Combination Sum II
tags: [medium, leedcode, Backtracking, c++]

---

# LC 40. Combination Sum II

### [Problem link](https://leetcode.com/problems/combination-sum-ii/)

###### tags: `leedcode` `medium` `c++` `Backtracking`

Given a collection of candidate numbers (<code>candidates</code>) and a target number (<code>target</code>), find all unique combinations in <code>candidates</code>where the candidate numbers sum to <code>target</code>.

Each number in <code>candidates</code>may only be used  **once**  in the combination.

 **Note:** The solution set must not contain duplicate combinations.

**Example 1:** 

```
Input: candidates = [10,1,2,7,6,1,5], target = 8
Output: 
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
```

**Example 2:** 

```
Input: candidates = [2,5,2,1,2], target = 5
Output: 
[
[1,2,2],
[5]
]
```

 **Constraints:** 

- <code>1 <=candidates.length <= 100</code>
- <code>1 <=candidates[i] <= 50</code>
- <code>1 <= target <= 30</code>

## Solution 1
#### C++
```cpp=
class Solution {
public:
    vector<vector<int>> res;

    void backtracking(vector<int> &candidates, int target, vector<int> &path, int &pathSum, int startIdx) {
        if (pathSum == target) {
            res.push_back(path);
            return;
        }
            
        for (int i = startIdx; i < candidates.size() && pathSum + candidates[i] <= target; i++) {
            if (i > startIdx && candidates[i] == candidates[i - 1]) {
                continue;
            }
            path.push_back(candidates[i]);
            pathSum += candidates[i];
            backtracking(candidates, target, path, pathSum, i + 1);
            path.pop_back();
            pathSum -= candidates[i];
        }
    }

    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<int> path;
        int pathSum = 0;
        sort(candidates.begin(), candidates.end());
        backtracking(candidates, target, path, pathSum, 0);
        return res;
    }
};
```
精簡版, 將pathSum的概念用target實現
```cpp=
class Solution {
public:
    vector<vector<int>> res;

    void backtracking(vector<int> &candidates, int target, vector<int> &path, int startIdx) {
        if (target == 0) {
            res.push_back(path);
            return;
        }
            
        for (int i = startIdx; i < candidates.size() && target - candidates[i] >= 0; i++) {
            if (i > startIdx && candidates[i] == candidates[i - 1]) {
                continue;
            }
            path.push_back(candidates[i]);
            backtracking(candidates, target - candidates[i], path, i + 1);
            path.pop_back();
        }
    }

    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<int> path;
        sort(candidates.begin(), candidates.end());
        backtracking(candidates, target, path, 0);
        return res;
    }
};
```

>### Complexity
>n = candidates.length
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n * 2^n)      | O(n * 2^n)       |

## Note
x
