In this Leetcode Next Permutation problem solution Implement the next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order). The replacement must be in place and use only constant extra memory.

Leetcode Next Permutation problem solution


Problem solution in Python.

class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        
        lenn = len(nums)
        
        if lenn < 2:
            return
        
        a = [nums[-1]]
        
        for i in range(lenn-2,-1,-1):
            if nums[i] < nums[i+1]:
                m = i+1
                for j in range(i+1,lenn):
                    if nums[j] > nums[i]:
                        if nums[j] < nums[i+1]:
                            m = j
                
                nums[i], nums[m] = nums[m], nums[i]
                
                nums[i+1:] = sorted(nums[i+1:])
                
                return
        
        for i in range(lenn//2):
            nums[i],nums[lenn-1-i] = nums[lenn-1-i],nums[i]



Problem solution in Java.

public static void nextPermutation(int[] nums) {
            if (nums == null || nums.length == 0) return;

            int maxElementIndex = nums.length-1;
            for (int i=nums.length-2;i>=0;i--) {
                if (nums[maxElementIndex]< nums[i]) maxElementIndex = i;
                if(nums[i] < nums[i+1]) {
                    for (int j=nums.length-1;j>i;j--) {
                        if (nums[j] > nums[i]){
                            maxElementIndex = j;
                            break;
                        }
                    }
                    swap(nums, i, maxElementIndex);
                    Arrays.sort(nums, i+1, nums.length);
                    return;
                }
            }

            Arrays.sort(nums);
        }

        private static void swap(int[] nums, int i, int j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }


Problem solution in C++.

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


Problem solution in C.

void nextPermutation(int* nums, int numsSize)
{
    int i, j;
    int t = -1;
    for (i = numsSize - 1; i > 0; --i) {
        if (nums[i - 1] < nums[i]) {
            t = i - 1;
            break;
        }
    }
    for (i = t + 1, j = numsSize - 1; i < j; ++i, --j) {
        nums[i] ^= nums[j];
        nums[j] ^= nums[i];
        nums[i] ^= nums[j];
    }
    if (t >= 0) {
        for (i = t + 1; i < numsSize; ++i) {
            if (nums[i] > nums[t]) {
                nums[i] ^= nums[t];
                nums[t] ^= nums[i];
                nums[i] ^= nums[t];
                break;
            }
        }
    }
}