Given a collection of numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
[1,2,3]
, [1,3,2]
, [2,1,3]
, [2,3,1]
, [3,1,2]
, and [3,2,1]
.
思路:递归咯
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
vector<int> path;
scan( nums, path, res);
return res;
}
void scan(vector<int>& nums, vector<int>& path, vector<vector<int>> &res){
if (path.size() == nums.size()){
res.push_back(path);
return;
}
for (int i = 0; i < nums.size(); i++){
if (find(path.begin(),path.end(), nums[i]) == path.end()){
path.push_back(nums[i]);
scan(nums, path, res);
path.pop_back();
}
}
}
};