下一个排列

问题描述:

给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列。

如果没有下一个排列,则输出字典序最小的序列。

Ex:

左边是原始排列,右边是对应的下一个排列。
1,2,3 → 1,3,2

3,2,1 → 1,2,3

1,1,5 → 1,5,1

思路分析:

刚开始看这个题目没有看懂,在网上搜集一番资料后,懂得了题目想要做的事情。
给出数字序列的全排列,按照字典序排好,找到对应的下一个排列。
比如:[1,1,5,4,2,7,6,4,3]—[1,1,5,4,3,2,4,6,7]
观察规律得出,原始数组从后往前看,找到第一个nums[i] < nums[i + 1]的索引位置,然后与数组从后往前的第一个大于nums[index]的数字交换,然后再将index后面的数逆序即可,从而得到下一个排列。
但是要注意特殊情况,如果找不到对应的索引,那么说明是全排列的最后一个排列了,只需将原数组整体逆序就可以了。
搞清楚的基本规律,接下来就是代码逻辑了。

Code:

public class Solution {
    /**
     * @param nums: an array of integers
     * @return: return nothing (void), do not return anything, modify nums in-place instead
     */
    public void nextPermutation(int[] nums) {
        // write your code here
        if (nums == null || nums.length == 0) {
            return;
        }
        int index = -1;
        //找到那个开始递减位置的索引,找不到索引为-1
        for (int i = nums.length - 2; i >= 0; i--) {
            if (nums[i] < nums[i + 1]){
                index = i;
                break;
            }
        }
        //如果已经是全排列的最后一个排列了,整体逆序
        if (index == -1) {
            reverse(nums, 0, nums.length - 1);
            return;
        }

        int biggerIndex = index + 1;
        //从后往前,找到第一个大于第一步找出的那个数
        for (int i = nums.length -1; i > index; i--) {
            if (nums[i] > nums[index]){
                biggerIndex = i;
                break;
            }
        }

        //交换第一步和第二步对应位置的元素
        int temp = nums[index];
        nums[index] = nums[biggerIndex];
        nums[biggerIndex] = temp;

        //反转剩下的元素
        reverse(nums, index + 1, nums.length - 1);

    }
    private void reverse(int[] nums, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }
    }
}

代码小评:

思路中的一步,这里分解成了三步。找到第一步索引,找到第二步索引,交换。用break语句及时跳出循环,说明已经找到。注意这里nums[i] > nums[i + 1], 索引从nums.length - 2开始的写法,而不是nums[i - 1] > nums[i],为了写的好看,并且防止越界。
但是这个题目用C++写会优美一些。

C++版本

class Solution {
public:
    /**
     * @param nums: a vector of integers
     * @return: return nothing (void), do not return anything, modify nums in-place instead
     */
    void nextPermutation(vector<int> &nums) {
        // write your code here
        int index = -1;
        for (int i = nums.size() - 2; i >= 0; i--) {
            if (nums[i] < nums[i + 1]) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            reverse(nums.begin(), nums.end());
            return;
        }

        int biggerIndex = index + 1;
        for (int i = nums.size() - 1; i > index; i--) {
            if (nums[i] > nums[index]) {
                biggerIndex = i;
                break;
            }
        }

        swap(nums[index], nums[biggerIndex]);
        reverse(nums.begin() + index + 1, nums.end());


    }
};

代码小评:

C++的STL库以及迭代器帮助解决了交换和逆序的步骤,让代码更加优雅,所以利用工具是很重要的。

作弊版本

class Solution {
public:
    /**
     * @param nums: a vector of integers
     * @return: return nothing (void), do not return anything, modify nums in-place instead
     */
    void nextPermutation(vector<int> &nums) {
        // write your code here
        next_permutation(nums.begin(), nums.end());
    }
};

代码小评:

直接利用STL库中的下一个排列函数,运行时间一致,说明该函数内部实现和我写的基本一致,所以熟悉STL库以及看看STL源码还是不错的。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值