forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31. Next Permutation.cpp
50 lines (30 loc) · 1.16 KB
/
31. Next Permutation.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
44
45
46
47
48
49
50
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int n=nums.size();
if(n==0 || n==1) return;
int index1=0, index2=0;
for(int i=n-2;i>=0;i--){
if(nums[i]<nums[i+1]){
index1=i;
break;
}
}
for(int i=n-1;i>=0;i--){
if(nums[i]>nums[index1]){
index2=i;
break;
}
}
swap(nums[index1], nums[index2]);
if(index1==index2) index1-=1; // list in reverse order, so we want to reverse list form index1-1 and not form index1+1, so subtracting 1
reverse(nums.begin()+index1+1, nums.end());
}
};