# 组合总和 II

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[[1, 7],[1, 2, 5],[2, 6],[1, 1, 6]]

示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[[1,2,2],[5]]
以下程序实现了这一功能,请你填补空白处内容: ```cpp #include using namespace std; class Solution { public: vector> combinationSum2(vector &candidates, int target) { vector> res; sort(candidates.begin(), candidates.end()); dfs(candidates, 0, target, res); return res; } private: vector stack; void dfs(vector &candidates, int start, int target, vector> &res) { if (target < 0) { return; } else if (target == 0) { res.push_back(stack); } else { int last = INT_MIN; for (int i = start; i < candidates.size(); i++) { if (last != candidates[i]) { stack.push_back(candidates[i]); _________________________________; stack.pop_back(); } last = candidates[i]; } } } }; ``` ## template ```cpp #include using namespace std; class Solution { public: vector> combinationSum2(vector &candidates, int target) { vector> res; sort(candidates.begin(), candidates.end()); dfs(candidates, 0, target, res); return res; } private: vector stack; void dfs(vector &candidates, int start, int target, vector> &res) { if (target < 0) { return; } else if (target == 0) { res.push_back(stack); } else { int last = INT_MIN; for (int i = start; i < candidates.size(); i++) { if (last != candidates[i]) { stack.push_back(candidates[i]); dfs(candidates, i + 1, target - candidates[i], res); stack.pop_back(); } last = candidates[i]; } } } }; ``` ## 答案 ```cpp dfs(candidates, i + 1, target - candidates[i], res); ``` ## 选项 ### A ```cpp dfs(candidates, i - 1, target - candidates[i], res); ``` ### B ```cpp dfs(candidates, i, target + candidates[i], res); ``` ### C ```cpp dfs(candidates, i + 1, target + candidates[i], res); ```