Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
解题代码:
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;//结果
vector<bool> temp(nums.size(), false);//标志
vector<int> resm;//单个结果
permute(nums, res, temp, resm);
return res;
}
private:
void permute(vector<int>& nums, vector<vector<int>>& res, vector<bool> temp, vector<int> resm){
if(resm.size() == nums.size())
{
res.push_back(resm);
}
else
{
for(int i = 0; i < nums.size(); i++)
{
if(temp[i] == true)
continue;
resm.push_back(nums[i]);
temp[i] = true;
permute(nums, res, temp, resm);
temp[i] = false;
resm.pop_back();
}
}
}
};