46. Permutations
2018-08-17 本文已影响0人
刘小小gogo
image.png
解法一:深度优先搜索
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> result;
if(nums.empty()){
return result;
}
vector<int> list;
dfs(result, list, nums);
return result;
}
private:
void dfs(vector<vector<int>> &result, vector<int> &list, vector<int> nums){
if(list.size() == nums.size()){
result.push_back(list);
return;
}
for(int i = 0; i < nums.size(); i++){
if(find(list.begin(), list.end(), nums[i]) != list.end()){
continue;
}
list.push_back(nums[i]);
dfs(result, list, nums);
list.pop_back();
}
return;
}
};