给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/permutations
class Solution { public: vector<vector<int>> res; void backtrack(vector<int>& track,vector<int> nums) { if(track.size()==nums.size()) { res.push_back(track); return; } for(int i=0;i<nums.size();i++) { vector<int>:: iterator iter=find(track.begin(),track.end(),nums[i]); if(iter!=track.end()) continue; track.push_back(nums[i]); backtrack(track,nums); track.pop_back(); } } vector<vector<int>> permute(vector<int>& nums) { vector<int> track; backtrack(track,nums); return res; } };