Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
题目链接:https://leetcode.com/problems/permutations/
体会:很熟,熟能生巧,毕竟是很简单的题
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
fun(res,nums,0,nums.size());
return res;
}
void fun(vector<vector<int>> &res,vector<int> nums,int n,int len)
{
if(n==len)
res.push_back(nums);
else
{
for(int i=n;i<len;i++)
{
swap(nums[n],nums[i]);
fun(res,nums,n+1,nums.size());
}
}
}
};