Javascript solution (150ms)


  • 0
    L
    var nextPermutation = function(nums) {
        var pivot = nums.length - 1;
        do{}while(nums[pivot] <= nums[--pivot]);
    
        for(var successor = nums.length - 1; successor > pivot; successor--){
            if(nums[successor] > nums[pivot]) break;
        }
        if(pivot != successor){
        var b = nums[pivot];
        nums[pivot] = nums[successor];
        nums[successor] = b;
        }
        nums.push.apply(nums, nums.splice(pivot+1).reverse());
    };

Log in to reply
 

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