# 组合总和 II

<p>给定一个数组&nbsp;<code>candidates</code>&nbsp;和一个目标数&nbsp;<code>target</code>&nbsp;，找出&nbsp;<code>candidates</code>&nbsp;中所有可以使数字和为&nbsp;<code>target</code>&nbsp;的组合。
</p>
<p><code>candidates</code>&nbsp;中的每个数字在每个组合中只能使用一次。</p>
<p><strong>说明：</strong></p>
<ul>
    <li>所有数字（包括目标数）都是正整数。</li>
    <li>解集不能包含重复的组合。&nbsp;</li>
</ul>
<p><strong>示例&nbsp;1:</strong></p>
<pre><strong>输入:</strong> candidates =&nbsp;[10,1,2,7,6,1,5], target =&nbsp;8,<strong><br />所求解集为:</strong>[[1, 7],[1, 2, 5],[2, 6],[1, 1, 6]]</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong> candidates =&nbsp;[2,5,2,1,2], target =&nbsp;5,<strong><br />所求解集为:</strong>[[1,2,2],[5]]</pre>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	vector<vector<int>> combinationSum2(vector<int> &candidates, int target)
	{
		vector<vector<int>> res;
		sort(candidates.begin(), candidates.end());
		dfs(candidates, 0, target, res);
		return res;
	}
private:
	vector<int> stack;
	void dfs(vector<int> &candidates, int start, int target, vector<vector<int>> &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 <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	vector<vector<int>> combinationSum2(vector<int> &candidates, int target)
	{
		vector<vector<int>> res;
		sort(candidates.begin(), candidates.end());
		dfs(candidates, 0, target, res);
		return res;
	}
private:
	vector<int> stack;
	void dfs(vector<int> &candidates, int start, int target, vector<vector<int>> &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);
```