Next Permutation

来源:互联网 发布:相片排版软件 编辑:程序博客网 时间:2024/04/30 04:38

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
1,1,5 → 1,5,1


Analysis: Search from the end to the beginning of the array. Search for a position of i, for which we can find one or more elements in the sub array num[i+1 ... num.length-1]. Choose the smallest qualified number in num[i+1 ... num.length-1] which is greater than num[i] and then swap these two numbers in place. After that, sort the sub array num[i+1 ... num.length-1] in ascending order. If we cannot find the number to be swapped after iterating the entire array, such array must be in descending order. 

public class Solution {    public void nextPermutation(int[] num) {        for(int i=num.length-2; i>=0; i--) {            int minVal = Integer.MAX_VALUE;            int minPosition = -1;            for(int j=i+1; j<num.length; j++) {                if(num[j]<minVal && num[j]>num[i]) {                    minVal = num[j];                    minPosition = j;                }            }                            if(minPosition != -1) {                // swap                num[i] = num[i] - num[minPosition];                num[minPosition] = num[i] + num[minPosition];                num[i] = num[minPosition] - num[i];                                // insertion sort the rest of the elements after i                for(int q=i+2; q<num.length; q++) {                    int temp=num[q], k=q-1;                    while(num[k]>=temp && k>=i+1) {                        num[k+1] = num[k];                        k--;                    }                    num[k+1] = temp;                }                return;            }        }                // the original array is in descending order        Arrays.sort(num);        return;    }}


Update: 30/01/2014

public class Solution {    public void swap(int[] num, int i, int minPosition) {        int temp = num[i];        num[i] = num[minPosition];        num[minPosition] = temp;return;}public void insertionSort(int[] num, int start) {for(int i=start+1; i<num.length; i++) {int temp=num[i], j=i-1;for( ; j>=start; j--) {if(num[j]>temp) num[j+1]=num[j];else break;}num[j+1] = temp;}return;}public void nextPermutation(int[] num) {        for(int i=num.length-2; i>=0; i--) {            int minVal = Integer.MAX_VALUE;            int minPosition = -1;            for(int j=i+1; j<num.length; j++) {                if(num[j]<minVal && num[j]>num[i]) {                    minVal = num[j];                    minPosition = j;                }            }if(minPosition != -1) {swap(num, i, minPosition);              // swap such two numbersinsertionSort(num, i+1);      // sort the digits after the swaped onereturn;}}                // the original array is in descending order        Arrays.sort(num);        return;    }}

0 0
原创粉丝点击