-
Notifications
You must be signed in to change notification settings - Fork 64
/
PermutationsII.cpp
53 lines (50 loc) · 1.25 KB
/
PermutationsII.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution {
public:
set<vector<int>> pu(vector<int>nums)
{
if(nums.size()==1)
{
vector<int>v;
v.push_back(nums[0]);
set<vector<int>>ans;
ans.insert(v);
return ans;
}
vector<int>v;
for(int i=1;i<nums.size();i++)
{
v.push_back(nums[i]);
}
set<vector<int>>s= pu(v);
set<vector<int>>ans;
int i=0;
for(auto val:s){
//val is a vector
vector<int>vale;
vale.push_back(nums[0]);
for(auto valr:val){vale.push_back(valr);}
ans.insert(vale);
for(int i=0;i<vale.size()-1;i++)
{
int a=vale[i];
int b=vale[i+1];
int temp=a;
a=b;
b=temp;
vale[i]=a;
vale[i+ 1]=b;
ans.insert(vale);
}
}
return ans;
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>>v;
set<vector<int>>ans=pu(nums);
for(auto val:ans)
{
v.push_back(val);
}
return v;
}
};