LeetCode 31 Next Permutation(下一个排列)

翻译

实现“下一个排列”函数,将排列中的数字重新排列成字典序中的下一个更大的排列。

如果这样的重新排列是不可能的,它必须重新排列为可能的最低顺序(即升序排序)。

重排必须在原地,不分配额外的内存。

以下是一些示例,左侧是输入,右侧是输出:

1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

原文

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,31,3,2
3,2,11,2,3
1,1,51,5,1

题意

哎,我那捉急的英语水平让我都看不懂题目,只能去翻谷歌翻译了……

什么字典序,又不是字母……排列?什么鬼……

后来才发现原来是数学中的排列组合,比如“1,2,3”的全排列,依次是:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

所以题目的意思是,从上面的某一行重排到期下一行,如果已经是最后一行了,则重排成第一行。

但是也不能根据给出的数组中的数字列出所有排列,因为要求不能占用额外的空间。

分析

网上看来一个示例,觉得挺好的,也没必要另外找一个了。

6 5 4 8 7 5 1

一开始没看对方的后面介绍,就自己在想这个排列的下一个排列是怎样的。

首先肯定从后面开始看,1和5调换了没有用。

7、5和1调换了也没有效果,因此而发现了8、7、5、1是递减的。

如果想要找到下一个排列,找到递增的位置是关键。

因为在这里才可以使其增长得更大。

于是找到了4,显而易见4过了是5而不是8或者7更不是1。

因此就需要找出比4大但在这些大数里面最小的值,并将其两者调换。

那么整个排列就成了:6 5 5 8 7 4 1

然而最后一步将后面的8 7 4 1做一个递增。

代码

根据上面的思路,在记事本上一气呵成写了如下代码:

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int index = nums.size() - 1;
        while(nums[index] < nums[index-1]) {
            --index;
        }
        if(index == 0) {
            sort(nums.begin(), nums.end());
            return ;
        }

        int second = INT_MAX, secondIndex = INT_MAX;
        for(int i = nums.size() - 1; i >= index - 1; -- i) {
            if(nums[i] > nums[index - 1]) {
                if(nums[i] < second) {
                    second = nums[i];
                    secondIndex = i;
                }               
            }               
        }       
        int temp = nums[index - 1];
        nums[index - 1] = nums[secondIndex];
        nums[secondIndex] = temp;

        sort(nums.begin()+index, nums.end());       
    }
}

提交到LeetCode上,发现错了……最后一行少了一个分号,加上之后还是错了……

没办法还是老老实实写到CodeBlocks上调试,加上一个符号就对了(下面的 = ):

while(nums[index] <= nums[index-1]) {
            --index;
}

于是接下来再看看别人的解法,有如下3个区别:

while(index > 0){
   if(num[index] > num[index - 1]){
       break;
   }
   index--;
}
int exchangeIndex;
for(int i = num.size()-1; i >= index; i--){
   if(num[i] > num[index-1]){
       exchangeIndex = i;
       break;
   }
}
swap(num[index-1],num[exchangeIndex]);

while循环这一部分我的更简单,不过其他的就要复杂些了,swap都没想到……

扩展

在C++标准库中还有一个函数next_permutation,其正是用于求一个数字序列的下一个排序。

void nextPermutation(vector<int> &num) {  
    next_permutation( num.begin(), num.end() );  
}  

再接再厉……

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值