-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46.cpp
More file actions
38 lines (37 loc) · 971 Bytes
/
46.cpp
File metadata and controls
38 lines (37 loc) · 971 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
37
38
class Solution {
public:
void 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;
}
bool check(vector<int>& a, vector<int>& b){
int n = a.size();
for(int i=0; i<n; i++)
if(a[i]!=b[i])
return true;
return false;
}
vector<vector<int>> permute(vector<int>& nums) {
vector<int> tmp = nums;
vector<vector<int>> res;
do{
res.push_back(nums);
nextPermutation(nums);
}while(check(tmp,nums));
return res;
}
};