class Solution {
public:
void nextPermutation(vector<int>& nums) {
int n=nums.size()-1;
bool flag=false;
for(int i=0;i<n;i++)
{
if(nums[i]<nums[i+1])
{
flag=true;
break;
}
}
//check
if(flag)
{
int k;
for(int i=n-1;i>=0;i--)
{
if(nums[i]<nums[i+1])
{
k=i;
break;
}
}
int j;
for(int i=n;i>k;i--)
{
if(nums[i]>nums[k])
{
j=i;
break;
}
}
swap(nums[k],nums[j]);
reverse(nums.begin()+k+1,nums.end());
}else
{
reverse(nums.begin(),nums.end());
}
}
};
Code Example |
---|
Cpp :: find substring in string c++ |
Cpp :: c++ get whole line |
Cpp :: check even or odd c++ |
Cpp :: insert element in array c++ |
Cpp :: swap in cpp |
Cpp :: how to format decimal palces in c++ |
Cpp :: explicit c++ |
Cpp :: define vector with size and value c++ |
Cpp :: polymorphism in c++ |
Cpp :: run c++ program mac |
Cpp :: why do we use pointers in c++ |
Cpp :: pass map as reference c++ |
Cpp :: c++ find index of element in array |
Cpp :: how to check char array equality in C++ |
Cpp :: cpp gui |
Cpp :: Syntax for C++ Operator Overloading |
Cpp :: Initialize Vector Iterator |
Cpp :: initialize 2d vector c++ |
Cpp :: accumulate in cpp |
Cpp :: stoi in c++ |
Cpp :: c/c++ windows api socket wrappers |
Cpp :: files in c++ |
Cpp :: How to pass a multidimensional array to a function in C and C++ |
Cpp :: pointers c++ |
Cpp :: operator overloading in c++ |
Cpp :: phi function (n log (log(n))) |
Cpp :: c++ main function parameters |
Cpp :: c++ allocate dynamic with initial values |
Cpp :: ue4 endoverlap c++ |
Cpp :: error C2011 |