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() );  
}  

再接再厉……

目录
相关文章
|
6月前
【Leetcode -441.排列硬币 -448.找到所有数组中消失的数字】
【Leetcode -441.排列硬币 -448.找到所有数组中消失的数字】
26 0
|
6月前
|
程序员
【Leetcode】面试题 01.02. 判定是否互为字符重排、面试题 01.04. 回文排列
目录 面试题 01.02. 判定是否互为字符重排 面试题 01.04. 回文排列
34 0
|
3月前
|
Go
golang力扣leetcode 937.重新排列日志文件
golang力扣leetcode 937.重新排列日志文件
27 0
|
3月前
|
Go
golang力扣leetcode 31.下一个排列
golang力扣leetcode 31.下一个排列
20 0
|
3月前
|
Go
golang力扣leetcode 567.字符串的排列
golang力扣leetcode 567.字符串的排列
20 0
|
4月前
|
算法
【LeetCode】31. 下一个排列【中等】
【LeetCode】31. 下一个排列【中等】
|
8月前
|
算法
LeetCode 算法 | 如何排列硬币?
LeetCode 算法 | 如何排列硬币?
|
10月前
|
算法 安全 Swift
LeetCode - #60 排列序列
不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。如果大家有建议和意见欢迎在文末留言,我们会尽力满足大家的需求。
|
10月前
|
算法 安全 Swift
LeetCode - #31 下一个排列 (Top 100)
不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。如果大家有建议和意见欢迎在文末留言,我们会尽力满足大家的需求。
|
10月前
|
数据安全/隐私保护
LeetCode 1734. 解码异或后的排列
LeetCode 1734. 解码异或后的排列
48 0

热门文章

最新文章