2014年3月9日星期日

Next Permutation

 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,31,3,2
3,2,11,2,3
1,1,51,5,1



Have you been asked this question in an interview?

public class Solution {
    public void nextPermutation(int[] num) {
        // find the last ascend
        int ascend = -1;
        int smallBig = -1;
        for(int i=0;i<num.length;i++) {
            if(i!=num.length-1 && num[i]<num[i+1]) {
                ascend = i;
            }
        }
        if(ascend == -1) {
            for(int i=0;i<num.length/2;i++) {
                int tmp = num[i];
                num[i] = num[num.length-1-i];
                num[num.length-1-i] = tmp;
            }
        } else {
            for(int i=num.length-1;i>=0;i--) {
                if(num[i]>num[ascend]) {
                    smallBig = i;
                    break;
                }
            }
            
            
            int tmp = num[smallBig];
            num[smallBig] = num[ascend];
            num[ascend] = tmp;
            for(int i=ascend+1;i<num.length;i++) {
                if(i<num.length-(i-ascend)) {
                    tmp = num[i];
                    num[i] = num[num.length-(i-ascend)];
                    num[num.length-(i-ascend)] = tmp;
                } else {
                    break;
                }
            }
        }
    }
}


Next Permutation
public class Solution {
    public void nextPermutation(int[] num) {
        if(num==null || num.length==0) return;
        // find first increase from the end
        int d = -1;
        for(int i=num.length-2;i>=0;i--) {
            if(num[i]<num[i+1]) {
                d = i;
                break;
            }
        }
        if(d==-1) {
            Arrays.sort(num);
            return;
        }
        int min = Integer.MAX_VALUE;
        int minid = 0;
        for(int i=d+1;i<num.length;i++) {
            if(num[i]>num[d] && num[i]<min) {
                min = num[i];
                minid = i;
            }
        }
        // swap
        int tmp = num[d];
        num[d] = num[minid];
        num[minid] = tmp;
        // sort
        int s = d+1;
        int e = num.length-1;
        Arrays.sort(num, s, e+1);
    }
}

=============

public class Solution {
    public void nextPermutation(int[] nums) {
        int firstIncrease = -1;
        for (int i = nums.length - 2; i >= 0; i--) {
            if (nums[i] < nums[i + 1]) {
                firstIncrease = i;
                break;
            }
        }
        if (firstIncrease == -1) {
            reverse(nums, 0, nums.length - 1);
            return;
        }
        int swapIndex = firstIncrease + 1;
        for (int i = firstIncrease + 1; i < nums.length; i++) {
            if (nums[i] > nums[firstIncrease] && nums[i] <= nums[swapIndex]) {
                swapIndex = i;
            }
        }
        int tmp = nums[swapIndex];
        nums[swapIndex] = nums[firstIncrease];
        nums[firstIncrease] = tmp;
        reverse(nums, firstIncrease + 1, nums.length - 1);
    }
   
    private void reverse(int num[], int start, int end) {
        while (start < end) {
            int tmp = num[start];
            num[start] = num[end];
            num[end] = tmp;
            start++;
            end--;
        }
    }
}

Input:[2,3,1,3,3]
Expected:[2,3,3,1,3]

没有评论:

发表评论