forked from arkingc/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
47.cpp
34 lines (26 loc) · 813 Bytes
/
47.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
permuteUnique(nums,0,res);
return res;
}
private:
void permuteUnique(vector<int>& nums,int idx,vector<vector<int>> &res){
if(idx == nums.size()){
res.push_back(nums);
}
set<int> s;
for(int i = idx;i < nums.size();i++){
if(s.find(nums[i]) != s.end()) continue;
s.insert(nums[i]);
int tp = nums[i];
nums[i] = nums[idx];
nums[idx] = tp;
permuteUnique(nums,idx + 1,res);
tp = nums[i];
nums[i] = nums[idx];
nums[idx] = tp;
}
}
};