previous and next permutation

previous and next permutation
2763541 (找最后一个正序35)
    2763541 (找3后面比3大的最后一个数4)
    2764531 (交换3,4的位置)
    2764135 (把4后面的5,3,1反转)
2764135 (找最后一个逆序41)
    2764135 (找4后面比4小的最后一个数3)
    2763145 (交换3,4的位置)
    2763541 (把3后面的1,4,5反转)

https://github.com/tongzhang1994/Facebook-Interview-Coding/blob/master/Previous%20Permutation.java

Next permutation
https://www.youtube.com/watch?v=w58KFpW5Pjk

class Solution {
    public void nextPermutation(int[] nums) {
        if(nums == null || nums.length == 0){
            return;
        }

        int replace = nums.length - 2;
        while(replace >= 0){
            if(nums[replace] < nums[replace + 1]){
                break;
            }else{
                replace--;
            }
        }

        if(replace < 0){
            Arrays.sort(nums);
            return;
        }

        int largeIndex = replace + 1;
        while(largeIndex < nums.length && nums[largeIndex] > nums[replace]){
            largeIndex++;
        }

        int tmp = nums[replace];
        nums[replace] = nums[largeIndex - 1];
        nums[largeIndex - 1] = tmp;
        Arrays.sort(nums, replace + 1, nums.length);

    }
}

比如对于725321来说,由于5321由于从最低位到最高位是升序排列,已经达到该四位数字permutation的最大值。这时不得不改变第5位的2来增加数值。如何改变?为了使增量最小,在前4位中比第5位大的数(5, 3)中找一个最小的数,即数字3。用3替换2,而剩下5, 2, 2, 1四个数字要组成最低4位。由于第5位已经从2增加到3,同样为了使增量最小,我们希望剩下的4位数尽可能小。所以将他们从低到高位降序排列即可。总结上面的思考:

1. 从低位向高位(从右向左)找第一个递减的数:s[i]<s[i+1]。如果不存在,则表明该permutation已经最大,next permutation为当前序列的逆序。
2. 在s[i+1:n-1]中找一个j,使s[j]>s[i]>=s[j+1],swap(s[i], s[j])
3. 将s[i+1:n-1]排序,从低位到高位单调递减。

Previous permutation 

// not tested yet

    class Solution {
        public int[] prev(int[] array) {
            int index = array.length - 2;
            while (index >= 0) {
                if (array[index] > array[index + 1]) {
                    break;
                } else {
                    index--;
                }
            }

            if (index < 0) {
                reverse(array, 0, array.length - 1);
                return array;
            }

            //else
            int smaller = index;
            while (smaller < array.length) {
                if (array[smaller] <= array[index]) {
                    smaller++;
                }
            }

            //
            int suitable = smaller - 1;

            // swap suitable with the num in index
            swap(array, suitable, index);

            // now reverse the leftover after the index
            // in descending order
            reverse(array, index + 1, array.length - 1);

            return array;
        }

        private void reverse(int[] array, int start, int end) {
            while (start < end) {
                swap(array,start,end);
            }
        }

        private void swap(int[] array, int i, int j){
          int tmp = array[i];
          array[i] = array[j];
          array[j] = tmp;
        }
    }
}

原文地址:https://www.cnblogs.com/tobeabetterpig/p/9490859.html

时间: 2024-11-13 08:48:52

previous and next permutation的相关文章

Next Permutation &amp; Previous Permutation

Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ascending order. Notice The list may contains duplicate integers. Example For [1,3,2,3], the next permutation is [1,3,3,2] For [4,3,2,1], the next per

leetcode_1053. Previous Permutation With One Swap

1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one-swap/ 题意:Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, t

LeetCode 1053. Previous Permutation With One Swap

原题链接在这里:https://leetcode.com/problems/previous-permutation-with-one-swap/ 题目: Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A

Permutation Index I &amp; II

Given a permutation which contains no repeated number, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1. Example Given [1,2,4], return 1. 分析:http://www.cnblogs.com/EdwardLiu/p/

Lintcode: Previous Permuation

Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Note The list may contains duplicate integers. Example For [1,3,2,3], the previous permutation is [1,2,3,3] For [1,2,3,4], the previous permutatio

lintcode-medium-Previous Permutation

Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Notice The list may contains duplicate integers. Example For [1,3,2,3], the previous permutation is [1,2,3,3] For [1,2,3,4], the previous permutat

31. 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 replaceme

Permutation Sequence

The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3

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 replaceme