-
Notifications
You must be signed in to change notification settings - Fork 69
/
nextPermutations.cpp
43 lines (39 loc) · 1.01 KB
/
nextPermutations.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
// A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
void swap(int i,int j,vector<int>& nums){
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
void reverse(int i,int j,vector<int>& nums){
while(i<j){
swap(i,j,nums);
i++;j--;
}
}
void nextPermutation(vector<int>& nums) {
int index1=-1,index2;
int n=nums.size();
for(int i=n-2;i>=0;i--){
if(nums[i+1]>nums[i]){
index1=i;
break;
}
}
if(index1==-1){
reverse(0,n-1,nums);
return;
}
for(int i=n-1;i>index1;i--){
if(nums[index1]<nums[i]){
index2=i;
break;
}
}
swap(index1,index2,nums);
reverse(index1+1,n-1,nums);
}
};