题目链接:https://leetcode.cn/problems/permutations/
思路:
只用题目给的nums数组进行递归,swap进行交换,注意还原现场
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>>ans;
f(nums,ans,0);
return ans;
}
void f(vector<int>nums,vector<vector<int>>&ans,int i)
{
if(i==nums.size())
{
ans.push_back(nums);
return;
}
for(int j=i;j<nums.size();j++)
{
swap(nums[i],nums[j]);
f(nums,ans,i+1);
swap(nums[i],nums[j]);
}
}
};
标签:排列,return,递归,nums,int,vector,ans
From: https://www.cnblogs.com/benscode/p/18659180