Simple yet best submission in C++


  • 1
    class Solution {
    public:
        void nextPermutation(vector<int>& nums) 
        {
            int size = nums.size();
            if(size < 2) return ;
            int i = size-2, j = size-1;
            while(i>=0 && nums[i]>=nums[i+1]) i--;
            if(i >= 0)
            {
                while(j>=0 && nums[j]<=nums[i]) j--;
                swap(nums[i], nums[j]);
            }
            reverse(nums.begin()+i+1, nums.end());
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.