27.Next Permutation(下一个字典序列)

Level:

??Medium

题目描述:

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

思路分析:

??这道题要求是给出当前序列的下一个序列(当前序列的下一个更大的序列),意思就是求当前序列的下一个字典序列。那么我们有以下算法:

??给定一个序列假如是a1,a2,a3,..ai,ai+1,ai+2..,aj..an
??1.找到最后一个正序序列ai,ai+1;
??2.找到ai后面最后一个比他大的数aj;
??3.交换ai和aj; a1,a2,a3,..aj,ai+1,ai+2..,ai..an
??4.将aj后面的所有数反转,即得到下一个序列,即下一个比它大的数

代码:

public class Solution{
    public void nextPermutation(int []nums){
        if(nums==null||nums.length==0)
            return;
        //第一步找到最后一对正序序列
        int flag=-1;
        for(int i=nums.length-1;i>=1;i--){
            if(nums[i]>nums[i-1]){
                flag=i-1;
                break;
            }
        }
       // 如果不存在正序,则证明该序列是由大到小的逆序,则反转整个序列
        if(flag==-1){
            reverse(nums,flag+1,nums.length-1);
            return;
        }
        //如果存在正序,则找到flag后面最后一个比它大的数,并交换
        for(int j=nums.length-1;j>flag;j--){
            if(nums[j]>nums[flag]){
                int temp=nums[j];
                nums[j]=nums[flag];
                nums[flag]=temp;
                break;
            }
        }
       //反转flag位置后的序列
        reverse(nums,flag+1,nums.length-1);
        return ;
        }
    public void reverse(int []nums,int start,int end){//反转序列
        while(start<end){
            int temp=nums[end];
            nums[end]=nums[start];
            nums[start]=temp;
            start++;
            end--;
        }
    }
}

原文地址:https://www.cnblogs.com/yjxyy/p/10772963.html

时间: 2024-08-29 09:53:25

27.Next Permutation(下一个字典序列)的相关文章

Leetcode:Next Permutation 下一个排列

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 ord

lintcode 中等题:next permutation下一个排列

题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] 注意 排列中可能包含重复的整数 解题 和上一题求上一个排列应该很类似 1.对这个数,先从右到左找到递增序列的前一个位置,peakInd 2.若peakInd = -1 这个数直接逆序就是答案了 3.peakInd>= 0 peakInd这个位置的所,和 peakInd 到nums.size() -1

【LeetCode每天一题】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

2.1.12 Next Permutation 下一个字典序数组

对当前排列从后向前扫描,找到一对为升序的相邻元素,记为i和j(i < j).如果不存在这样一对为升序的相邻元素,则所有排列均已找到,算法结束:否则,重新对当前排列从后向前扫描,找到第一个大于i的元素k,交换i和k,然后对从j开始到结束的子序列反转,则此时得到的新排列就为下一个字典序排列.这种方式实现得到的所有排列是按字典序有序的,这也是C++ STL算法next_permutation的思想.

31. Next Permutation 返回下一个pumutation序列

[抄题]: 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 rep

[LeetCode]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

leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

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 orde

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

[leetcode]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