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,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 numbers
				insertionSort(num, i+1);      // sort the digits after the swaped one
				return;
			}
		}
        
        // the original array is in descending order
        Arrays.sort(num);
        return;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值