LeetCode-31 Next Permutation

mplement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

The next permutation is a concept that I don’t know.
For a string, “ABCDEF” sorted in lexico order, there are many its permutations that follows.
For the next one, it should be “ABCDFE”,”ABCEFD”.. so on

There is a algorithm for this problem:
Next lexicographical permutation algorithm

Print all permutations in sorted (lexicographic) order

class Solution {
    public void nextPermutation(int[] nums) {

            int pivot = -1;
            for(int i=nums.length-1;i>0;i--){
                if(nums[i-1]<nums[i]) {
                    pivot=i-1;
                    break;
                }
            }
            if(pivot<0) {
                 Arrays.sort(nums);
            }else{
                int j=nums.length-1;
                while(j>-1&&nums[j]<=nums[pivot]){j--;}

                //swap(pivot, j);
                if(j>=0){
                    int tmp = nums[pivot];
                    nums[pivot] = nums[j];
                    nums[j]= tmp;

                    //sort(pivot+1,j);
                    Arrays.sort(nums,pivot+1,nums.length);
                }
            }
    }
}