# 40. Combination Sum II

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

Each number in `candidates` 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:

• `1 <= candidates.length <= 100`
• `1 <= candidates[i] <= 50`
• `1 <= target <= 30`

``````class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> cur;
sort(candidates.begin(), candidates.end());
dfs(candidates, target, 0, cur, res);
return res;
}
void dfs(vector<int>& candidates, int target, int start, vector<int>& cur, vector<vector<int>>& res) {
if (target < 0) return;
if (target == 0) { res.push_back(cur); return; }
for (int i = start; i < candidates.size(); ++i) {
if (i > start && candidates[i] == candidates[i - 1]) continue;
cur.push_back(candidates[i]);
dfs(candidates, target - candidates[i], i + 1, cur, res);
cur.pop_back();
}
}
};
``````

``````class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> res;
sort(candidates.begin(), candidates.end());
for (int i = 0; i < candidates.size(); ++i) {
if (candidates[i] > target) break;
if (candidates[i] == target) { res.push_back({candidates[i]}); break; }
if (i > 0 && candidates[i] == candidates[i - 1]) continue;
vector<int> vec = vector<int>(candidates.begin() + i + 1, candidates.end());
vector<vector<int>> tmp = combinationSum2(vec, target - candidates[i]);
for (auto a : tmp) {
a.insert(a.begin(), candidates[i]);
res.push_back(a);
}
}
return res;
}
};
``````

``````class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<vector<int>>> dp(target + 1);
dp[0].resize(1);
map<int, int> numCnt;
for (int num : candidates) {
++numCnt[num];
}
for (auto a : numCnt) {
int num = a.first, cnt = a.second;
for (int i = target - num; i >= 0; --i) {
for (auto v : dp[i]) {
int sum = i;
for (int k = 0; k < cnt && sum <= target - num; ++k) {
sum += num;
v.push_back(num);
dp[sum].push_back(v);
}
}
}
}
return dp[target];
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/40

Combination Sum III

Combination Sum

https://leetcode.com/problems/combination-sum-ii/

https://leetcode.com/problems/combination-sum-ii/discuss/16861/Java-solution-using-dfs-easy-understand

https://leetcode.com/problems/combination-sum-ii/discuss/16878/Combination-Sum-I-II-and-III-Java-solution-(see-the-similarities-yourself)

LeetCode All in One 题目讲解汇总(持续更新中…)

（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，快快加入吧～）

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation