# 全排列

<p>给定一个<strong> 没有重复</strong> 数字的序列，返回其所有可能的全排列。</p><p><strong>示例:</strong></p><pre><strong>输入:</strong> [1,2,3]<strong><br />输出:</strong>[  [1,2,3],  [1,3,2],  [2,1,3],  [2,3,1],  [3,1,2],  [3,2,1]]</pre>

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

```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	vector<vector<int>> permute(vector<int> &nums)
	{
		vector<vector<int>> res;
		vector<bool> used(nums.size());
		dfs(nums, used, res);
		return res;
	}
private:
	vector<int> stack;
	void dfs(vector<int> &nums, vector<bool> &used, vector<vector<int>> &res)
	{
		if (stack.size() == nums.size())
		{
			res.push_back(stack);
		}
		else
		{
			for (int i = 0; i < nums.size(); i++)
			{
				if (!used[i])
				{
					_______________;
				}
			}
		}
	}
};
```

## template

```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	vector<vector<int>> permute(vector<int> &nums)
	{
		vector<vector<int>> res;
		vector<bool> used(nums.size());
		dfs(nums, used, res);
		return res;
	}
private:
	vector<int> stack;
	void dfs(vector<int> &nums, vector<bool> &used, vector<vector<int>> &res)
	{
		if (stack.size() == nums.size())
		{
			res.push_back(stack);
		}
		else
		{
			for (int i = 0; i < nums.size(); i++)
			{
				if (!used[i])
				{
					used[i] = true;
					stack.push_back(nums[i]);
					dfs(nums, used, res);
					stack.pop_back();
					used[i] = false;
				}
			}
		}
	}
};
```

## 答案

```cpp
used[i] = true;
stack.push_back(nums[i]);
dfs(nums, used, res);
stack.pop_back();
used[i] = false;
```

## 选项

### A

```cpp
used[i] = false;
stack.push_back(nums[i]);
dfs(nums, used, res);
stack.pop_back();
used[i] = true;
```

### B

```cpp
stack.push_back(nums[i]);
dfs(nums, used, res);
stack.pop_back();
used[i] = true;
```

### C

```cpp
stack.push_back(nums[i]);
dfs(nums, used, res);
stack.pop_back();
```