-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31&&47
54 lines (50 loc) · 1.51 KB
/
31&&47
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
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
void nextPermutation(vector<int>& nums) {
if(nums.size()<2)return ;
int i;
for(i=nums.size()-2;i>=0;--i){
if(nums[i]<nums[i+1])break;
}
if(i>=0){
int k=i;
for(i=nums.size()-1;i>k;--i){
if(nums[i]>nums[k])break;
}
swap(nums[k],nums[i]);
reverse(nums.begin()+k+1,nums.end());
}else reverse(nums.begin(),nums.end());
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
if(nums.empty())return vector<vector<int>>{};
vector<vector<int>>ret;
vector<int>tmp;
vector<bool> used(nums.size(),false);
sort(nums.begin(),nums.end());
helper(ret,nums,tmp,used);
return ret;
}
void helper(vector<vector<int>>&ret,vector<int>&nums,vector<int>&tmp,vector<bool> used){
if(tmp.size()==nums.size()){
ret.push_back(tmp);
return ;
}
for(int i=0;i<nums.size();++i){
if(used[i])continue;
if(i>0&&used[i-1]==false&&nums[i-1]==nums[i])continue;
used[i]=true;
tmp.push_back(nums[i]);
helper(ret,nums,tmp,used);
used[i]=false;
tmp.pop_back();
}
}
int main(){
vector<int>v{1,2,4,6,5,3,2,1};
nextPermutation(v);
for(auto i: v)cout<<i<<",";
cout<<endl;
return 0;
}