#leetcode#Next Permuatation

Implement 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


看着是有规律但是智商有限总结不出, 查wiki:

The following algorithm generates the next permutation lexicographically after a given permutation. It changes the given permutation in-place.

  1. Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation.
  2. Find the largest index l greater than k such that a[k] < a[l].
  3. Swap the value of a[k] with that of a[l].
  4. Reverse the sequence from a[k + 1] up to and including the final element a[n].

For example, given the sequence [1, 2, 3, 4] which starts in a weakly increasing order, and given that the index is zero-based, the steps are as follows:

  1. Index k = 2, because 3 is placed at an index that satisfies condition of being the largest index that is still less than a[k + 1] which is 4.
  2. Index l = 3, because 4 is the only value in the sequence that is greater than 3 in order to satisfy the condition a[k] < a[l].
  3. The values of a[2] and a[3] are swapped to form the new sequence [1,2,4,3].
  4. The sequence after k-index a[2] to the final element is reversed. Because only one value lies after this index (the 3), the sequence remains unchanged in this instance. Thus the lexicographic successor of the initial state is permuted: [1,2,4,3].

Following this algorithm, the next lexicographic permutation will be [1,3,2,4], and the 24th permutation will be [4,3,2,1] at which point a[k] <a[k + 1] does not exist, indicating that this is the last permutation.


代码如下:

public class Solution {
    public void nextPermutation(int[] nums) {
        if(nums==null || nums.length==0)
            return;
        int i = nums.length-2;
        while(i >= 0 && nums[i] > nums[i + 1]){
            i--;
        }
        if(i >= 0){
            int j = i + 1;
            while(j < nums.length && nums[j] > nums[i]){
                j++;
            }
            j--;
            swap(nums[i], nums[j]);
        }
        reverse(nums, i + 1, nums.length - 1);
    }
    
    private void swap(int a, int b){
        int tmp = a; 
        a = b;
        b = tmp;
    }   
    
    private void reverse(int[] nums, int left, int right){
        while(left < right){
            swap(nums[left], nums[right]);
            left++;
            right--;
        }
    }
}

这里有个 致命bug,那就是我用了swap(int a, int b)来做数组中值的交换, 对Java pass by value的理解不行啊。。太傻比了, swap(num[ i ], num[ j ])也只是把index 为 i和j的两个int传入了swap method中, 在其他方法中怎么交换和原来数组没有影响!

关于 pass by value 的讲解:   http://javadude.com/articles/passbyvalue.htm

正确code:

public class Solution {
    public void nextPermutation(int[] nums) {
        if(nums==null || nums.length==0)
            return;
        int i = nums.length-2;
        while(i >= 0 && nums[i] >= nums[i + 1]){// >= 而不是 >, 因为有可能有duplicates, 比如 511
            i--;
        }
        if(i >= 0){
            int j = i + 1;
            while(j < nums.length && nums[j] > nums[i]){
                j++;
            }
            j--;
            // swap(nums[i], nums[j]);
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }
        reverse(nums, i + 1, nums.length - 1);
    } 
    
    private void reverse(int[] nums, int i, int j){
        while(i < j){
            // swap(nums[i], nums[j]);
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
            i++;
            j--;
        }
    }
}


upgrade on 06/29/2015

public class Solution {
    public void nextPermutation(int[] nums) {
        if(nums == null || nums.length < 2)
            return;
        
        int left;
        int right;
        
        for(left = nums.length - 2; left >= 0; left--){
            if(nums[left] < nums[left + 1]){
                break;
            }
        }
        
        if(left < 0){
            reverse(nums, 0, nums.length - 1);
            return;
        }
        
        for(right = nums.length - 1; right > left; right--){
            if(nums[right] > nums[left]){
                break;
            }
        }
        
        swap(nums, left, right);
        
        reverse(nums, left + 1, nums.length - 1);
    }
    
    private void swap(int[] a, int l, int r){
        int tmp = a[l];
        a[l] = a[r];
        a[r] = tmp;
    }
    
    private void reverse(int[] a, int l, int r){
        while(l < r){
            swap(a, l, r);
            l++;
            r--;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值