题目连接:https://leetcode-cn.com/problems/permutations
题目描述:
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
解题:
class Solution {
public:
vector<int> path;
vector<vector<int>> result;
void trackingBack(vector<int>& nums, vector<bool>& used)
{
if(path.size() == nums.size())
{
result.push_back(path);
return;
}
for(int i = 0; i < nums.size(); i++)
{
if(used[i] == true)
continue;
used[i] = true;
path.push_back(nums[i]);
trackingBack(nums, used);
path.pop_back();
used[i] = false;
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<bool> used(nums.size(), false);
trackingBack(nums, used);
return result;
}
};