力扣第46题 全排列
class Solution {
public:
void mySwap(vector<int> &nums, int l, int r)
{
int temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
}
void backtrack(int n, vector<int> &nums, vector<vector<int>>& res, int idx)
{
if (n == idx)
{
res.push_back(vector<int>(nums));
return;
}
for (int i = idx; i < n; i++)
{
mySwap(nums, i, idx);
backtrack(n, nums, res, idx + 1);
mySwap(nums, i, idx);
}
}
vector<vector<int>> permute(vector<int>& nums)
{
vector<vector<int>> res;
backtrack(nums.size(), nums, res, 0);
return res;
}
};