-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations_II.cpp
More file actions
36 lines (36 loc) · 927 Bytes
/
Permutations_II.cpp
File metadata and controls
36 lines (36 loc) · 927 Bytes
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
class Solution {
public:
void nextPermutation(vector<int> &A);
bool check(vector<int>& a, vector<int>& b){
for(int i=0; i<a.size();i++)
if(a[i]!=b[i])
return false;
return true;
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<int> curr = nums;
vector<vector<int>> res;
do{
res.push_back(curr);
nextPermutation(curr);
}while(!check(curr,nums));
return res;
}
};
void Solution::nextPermutation(vector<int> &A) {
int n = A.size(),i;
int invert = n-2;
while (invert>=0 && A[invert]>=A[invert+1]) invert--;
if (invert==-1){
reverse(A.begin(),A.end());
return;
}
for(i = n-1; i>=invert+1; i--){
if (A[i]>A[invert]){
swap(A[i],A[invert]);
break;
}
}
reverse(A.begin()+(invert+1),A.end());
return;
}