LeetCode-array-NextPermutation

題目: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

題目大意,給到一串?dāng)?shù)組,求下一排列順序,例如3,2,1 → 1,2,3,321已結(jié)是最大的了,所以下一排列就是123最小的,例如 [1,2,3]數(shù)組,的下一排列是[1,3,2]可以看出來(lái),好像從數(shù)組的后面判斷,如果前一位小于后一位將調(diào)換位置,再看數(shù)組是[1,2,4,3]那么下一排列正確答案是[1,3,2,4],可以分析出從后往前判斷,2小于4那么不能將2,4對(duì)換,換后并不是下一排列,而是下下下..排列了,應(yīng)該將2,4的下標(biāo)記錄后,從2的后面判斷最小大于2的數(shù)字是3,那么2,3對(duì)換成為[1,3,4,2],這時(shí)還得將后面的4,2重新小到大排列后得到最后結(jié)果
[1,3,2,4],OK。

java代碼如下

package LeetCode1;

import java.util.Arrays;

/**
 * Created by Wangjianxin on 2017/7/11 0011.
 */

public class NextPermutation {

    public static void main(String[] args) {
        int[] s = {2,3,1};
        nextPermutation(s);
    }

    public static void nextPermutation(int[] nums) {

        int len = nums.length;
        int leftindex = -1;
        int left = len - 2;
        int right = len - 1;
        //先從后往前找出i比i+1小的數(shù)字的下標(biāo)
        for(int i = len - 2; i >=0;--i){
            System.out.println(nums[right]);
            if (nums[left] < nums[right]){
                leftindex = left;
                break;
            }
            left -- ;
            right --;
        }
        System.out.println(leftindex);
        if(leftindex != -1){
            //找到后,從這個(gè)數(shù)之后找到最小大于這個(gè)數(shù)的元素
            int k = Integer.MAX_VALUE;
            int kindex = 0;
            for(int i = leftindex+1; i < len;i++){
                int min = nums[i] - nums[leftindex];
                if(k > min && min > 0){
                    k = min;
                    kindex = i;
                }
            }
            int temp = 0;
            temp =  nums[kindex];
            nums[kindex] = nums[leftindex];
            nums[leftindex] = temp;

            System.out.println(Arrays.toString(nums));
            sort(nums,leftindex + 1,len-1);
        }else{
         Arrays.sort(nums);
        }

        System.out.println(Arrays.toString(nums));
    }
    //這里的參數(shù)end沒(méi)有用到
    public static int[] sort(int[] s, int begin,int end){
        for(int i= begin ;i <s.length;i++){
            for(int j = begin ;j  < s.length -1; j++){
                if(s[j] > s[j+1]){
                    int temp = s[j];
                    s[j] = s[j+1];
                    s[j+1] = temp;
                }
            }
        }
        return s;
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容