题目描述
blablabla
样例
blablabla
算法1
DFS
C++ 代码
class Solution {
vector<vector<int>> res;
vector<int> onePermutation;
vector<bool> visited;
int n;
set<vector<int>> hashTable; //去重
public:
vector<vector<int>> permutation(vector<int>& nums) {
n = nums.size();
onePermutation = vector<int>(0);
visited = vector<bool>(n);
DFS(nums, 0, n, visited, onePermutation, res);
for(auto permu : hashTable) {
res.push_back(permu);
}
return res;
}
void DFS(vector<int>& nums, int pos, int n, vector<bool>& visited, vector<int>& onePermutation, vector<vector<int>>& res) {
if(pos == n) {
hashTable.insert(onePermutation);
return ;
}
for(int i = 0; i < n; ++i) {
if(visited[i] == false) {
onePermutation.push_back(nums[i]);
visited[i] = true;
DFS(nums, pos + 1, n, visited, onePermutation, res);
visited[i] = false;
onePermutation.pop_back();
}
}
}
};
大佬太nb了!!!!!