-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31.cpp
More file actions
40 lines (35 loc) · 837 Bytes
/
31.cpp
File metadata and controls
40 lines (35 loc) · 837 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
39
40
// Problem : 31. Next Permutation
// Link : https://leetcode.com/problems/next-permutation/
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int i,j,c=0;
for(i=nums.size()-1;i>0;i--)
{
if(nums[i-1]<nums[i])
{
c++;
j = i-1;
break;
}
}
if(c)
{
reverse(nums.begin()+j+1,nums.end());
int x = upper_bound(nums.begin()+j+1,nums.end(),nums[j]) - nums.begin();
swap(nums[x],nums[j]);
}
else
sort(nums.begin(),nums.end());
}
};
int main() {
Solution ob;
vector<int> nums = {1,2,3};
ob.nextPermutation(nums);
return 0;
}