-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path47. Permutations II
62 lines (55 loc) · 1.32 KB
/
47. Permutations II
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
54
55
56
57
58
59
60
61
62
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums)
{
int n = nums.size();
vector<vector<int>> ans;
ans.push_back(nums);
while(true)
{
int maxI=n-1, maxJ=n-1, a = 0;
for(int i=n-1; i>0; i--)
{
if(nums[i]>nums[i-1])
{
maxI = i;
a=1;
break;
}
}
for(int i=n-1; i>0; i--)
{
if(nums[i]>nums[maxI-1])
{
maxJ=i;
break;
}
}
if(a==0)
{
reverse(nums.begin(), nums.end());
}
else
{
swap(nums[maxJ], nums[maxI-1]);
reverse(nums.begin()+maxI, nums.end());
}
int p=1;
for(int i=0; i<n; i++)
{
if(ans[0][i]==nums[i])
{
continue;
}
else
{
p=0;
break;
}
}
if(p==1) break;
ans.push_back(nums);
}
return ans;
}
};