• 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 replacement must be in-place and use only constant 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

    一开始无从下手,因为不理解题目要干啥。然后查资料:

    next_permutation函数将按字母表顺序生成给定序列的下一个较大的排列,直到整个序列为降序为止。prev_permutation函数与之相反,是生成给定序列的上一个较小的排列。二者原理相同,仅遍例顺序相反

     

     

     

     

     

    问题:如何求一个数组的下一个排列?

     

     

     

    假设该数组为3 6 4 2.下一个排列应为 4 2 3 6.

     

    过程:对于一个任意序列,最小的排列是增序,最大的为减序。

     

    从最后一位向前看,首先得到的是2,单纯的一个数不需要进行交换。

     

    然后得到的是 4 2,4大于2,在这个子序列中已经为最大序列,无法排出更大的序列了。

     

    然后得到的是 6 4 2,原理同上。

     

    之后得到的是 3 6 4 2,此时由于3小于6且小于4,而 3 6 4 2的下一个排列应为比3 6 4 2这个排列大的排列中最小的那个,所以3应该和4进行交换,此时该排列变为 4 6 3 2.此时4位于首端,所以之后的序列应为最小序列,即 2 3 6.综上,最终结果应为 4 2 3 6.

    --------------------- 

    作者:HyJoker 

    来源:CSDN 

    原文:https://blog.csdn.net/hyjoker/article/details/50899362 

    版权声明:本文为博主原创文章,转载请附上博文链接!

    好的,现在已经有点明白了。

    至此,可以写code了

    class Solution {
        public void nextPermutation(int[] nums) {
            if(nums==null || nums.length==0) return;
            int i = nums.length-2;
            while(i>=0&&nums[i] >= nums[i+1])
                i--;
            if(i>=0){
            int j = i + 1;
            while(j < nums.length && nums[j] > nums[i])
                j++;
            j--;
            swap(nums,i,j);       
            }
             reverse(nums,i+1,nums.length-1);
        }
        public void swap(int[] nums, int i, int j){
            int t = nums[i];
            nums[i] = nums[j];
            nums[j] = t;
        }
        public void reverse(int[] nums, int i, int j){
            while(i<j){
                swap(nums, i++, j--);
            }
        }
    }
    class Solution {
        public void nextPermutation(int[] nums) {
            int n = nums.length;
            if(n == 1) return;
            int i = n - 1;
            while(i >= 1 && nums[i - 1] >= nums[i]) i--;
            if(i == 0) {
                reverse(nums, 0);
                return;
            }
            
            i--;
            
            int j = n - 1;
            while(j > i && nums[j] <= nums[i]) j--;
            
            swap(nums, i, j);
            reverse(nums, i+1);
        }
        
        public void reverse(int[] nums, int i) {
            int left = i, right = nums.length - 1;
            while(left < right) {
                swap(nums, left, right);
                left++;
                right--;
            }
        }
        
        public void swap(int[] nums, int i, int j) {
            nums[i] = nums[i] + nums[j];
            nums[j] = nums[i] - nums[j];
            nums[i] = nums[i] - nums[j];
        }
    }
  • 相关阅读:
    41.给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数。
    Java反射学习记录
    LeetCode算法笔记-回溯法
    LeetCode算法笔记(二)
    LeetCode算法笔记(一)
    JDBC学习笔记--通用的查询方法
    JDBC学习笔记--ResultSetMetaData
    JDBC学习笔记--PreparedStatement
    Java学习笔记---字符串
    Java学习笔记---通过异常处理错误
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/10416223.html
Copyright © 2020-2023  润新知