Leetcode31.Next Permutation

 public void nextPermutation(int[] nums) {
        if(nums == null || nums.length == 0)
            return;
        
        int len = nums.length;
        int i, j;
        
        for(i = len - 2; i >= 0; i--){
            if(nums[i] >= nums[i+1])
                continue;
            
            for(j = i+1; j < len && nums[j] > nums[i]; j++);
            swap(nums, i, j-1);
            reverse(nums, i+1);
            return;
        }
        
        reverse(nums, 0);
    }
    
    public void swap(int[] nums, int i, int j){
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
    
    public void reverse(int [] nums, int i){
        int j = nums.length - 1;
        
        while(i < j){
            swap(nums, i, j);
            i++;
            j--;
        }
    }

 

posted @ 2020-01-08 19:38  阿柒~  阅读(195)  评论(0编辑  收藏  举报