# 46.47.全排列问题

[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

``````class Solution {
public:
vector<vector<int>> ans;
vector<vector<int>> permute(vector<int>& nums) {
dfs(0, nums);
return ans;
}
void dfs(int index, vector<int>& output) {
if (index == output.size()) {
ans.push_back(output);
return;
}
for (int i = index; i < output.size(); i++) {
swap(output[i], output[index]);
dfs(index + 1, output);
swap(output[i], output[index]);
}
}
};``````

### 47.全排列Ⅱ

[
[1,1,2],
[1,2,1],
[2,1,1]
]

``````class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
if (nums.empty())   return {};
traceBack(nums, 0);
return result;
}
void traceBack(vector<int>& nums, int begin)
{
if (begin == nums.size())
{
result.push_back(nums);
return;
}
unordered_set<int> used;
for (int i = begin; i < nums.size(); i++)
{
if (used.find(nums[i]) != used.end())   continue;
used.insert(nums[i]);
swap(nums[begin], nums[i]);
traceBack(nums, begin + 1);
swap(nums[begin], nums[i]);
}
}
vector<vector<int>> result;
};``````