class Solution {
public:
void permutation(vector<int>& nums, int start, vector<vector<int>>& permu)
{
if(start == nums.size())
{
permu.push_back(nums);
}
for(int i = start;i<nums.size();i++)
{
//交换i和start,相当于固定start位置的值
int tmp = nums[i];
nums[i] = nums[start];
nums[start] = tmp;
permutation(nums, start+1, permu);
//还原之前的交换
tmp = nums[i];
nums[i] = nums[start];
nums[start] = tmp;
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> permu;
permutation(nums, 0, permu);
return permu;
}
};
LeetCode || Permutations
最新推荐文章于 2020-04-25 12:34:34 发布