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

这道题让我们求下一个排列顺序,由题目中给的例子可以看出来,如果给定数组是降序,则说明是全排列的最后一种情况,则下一个排列就是最初始情况,可以参见之前的博客 Permutations。再来看下面一个例子,有如下的一个数组

1  2  7  4  3  1

下一个排列为:

1  3  1  2  4  7

那么是如何得到的呢,我们通过观察原数组可以发现,如果从末尾往前看,数字逐渐变大,到了2时才减小的,然后再从后往前找第一个比2大的数字,是3,那么我们交换2和3,再把此时3后面的所有数字转置一下即可,步骤如下:

1    7  4  3  1

1    7  4    1

1    7  4    1

1  3  1  2  4  7

解法一:

class Solution {
public:
void nextPermutation(vector<int> &num) {
int i, j, n = num.size();
for (i = n - ; i >= ; --i) {
if (num[i + ] > num[i]) {
for (j = n - ; j > i; --j) {
if (num[j] > num[i]) break;
}
swap(num[i], num[j]);
reverse(num.begin() + i + , num.end());
return;
}
}
reverse(num.begin(), num.end());
}
};

下面这种写法更简洁一些,但是整体思路和上面的解法没有什么区别,参见代码如下:

解法二:

class Solution {
public:
void nextPermutation(vector<int>& nums) {int n = nums.size(), i = n - , j = n - ;
while (i >= && nums[i] >= nums[i + ]) --i;
if (i >= ) {
while (nums[j] <= nums[i]) --j;
swap(nums[i], nums[j]);
}
reverse(nums.begin() + i + , nums.end());
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/31

类似题目:

Permutations II

Permutations

Permutation Sequence

Palindrome Permutation II

Palindrome Permutation

参考资料:

https://leetcode.com/problems/next-permutation/

https://leetcode.com/problems/next-permutation/discuss/13921/1-4-11-lines-C%2B%2B

https://leetcode.com/problems/next-permutation/discuss/13867/C%2B%2B-from-Wikipedia

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 31. Next Permutation 下一个排列的更多相关文章

  1. [leetcode]31. Next Permutation下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

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

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

  3. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  4. 【LeetCode每天一题】Next Permutation(下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. LeetCode(31): 下一个排列

    Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...

  6. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. 031 Next Permutation 下一个排列

    实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...

  8. lintcode:next permutation下一个排列

    题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...

  9. 31. Next Permutation (下一个全排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. javascript播放图片序列帧

    javascript播放图片序列帧1 先预加载<pre>var load_img = [];for(k=0;k<=16;k++){load_img.push( '/cjsxy/ima ...

  2. 打开IDEA的更新选项,如何打开IDEA更新弹窗

    如何让IDEA的更新弹窗重新出现,打开IDEA的更新选项 IDEA  update的时候,会提示一个更新的弹框选择框如下图所示 在最下方有个Do not show this dialog in the ...

  3. 从零到一手写基于Redis的分布式锁框架

    1.分布式锁缘由 学习编程初期,我们做的诸如教务系统.成绩管理系统大多是单机架构,单机架构在处理并发的问题上一般是依赖于JDK内置的并发编程类库,如synchronize关键字.Lock类等.随着业务 ...

  4. Greenplum集群或者Postgresql出现死锁肿么办?

    1.Greenplum集群或者Postgresql出现死锁肿么办? 由于Postgresql和Greenplum集群这数据库知识很深的,没有仔细研究,遇到问题真的不知道肿么处理,我遇到死锁,是采取了暴 ...

  5. springboot2使用外部的tomcat服务器创建项目步骤

    使用内置的Servlet容器.应用打成可执行的jar.外置的Servlet容器:外面安装Tomcat---应用war包的方式打包: a).必须创建一个war项目:(利用idea创建好目录结构) b). ...

  6. SAP-简单的OALV演示练习

    接上一篇传统ALV:https://www.cnblogs.com/BruceKing/p/11320165.html. 首先介绍下什么是ALV,在R/3 4.6C之前ALV全称为ABAP List ...

  7. ASP.NET Core Web 项目文件

    在本节中,我们将探索并了解 asp.net core 项目文件. 我们使用 C#作为编程语言,因此项目文件具有.csproj 扩展名. 如果您使用过以前版本的 ASP.NET,那么您可能对此文件非常熟 ...

  8. Python如何实现单例模式?其他23中设计模式python如何实现?

    单例模式主要有四种方法:new.共享属性.装饰器.import. # __ new__方法: class Singleton(object): def __new__(cls, *args, **kw ...

  9. Asp.Net MVC强类型页面获取值几种方式

    方式一 (V:视图) @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="v ...

  10. i春秋-第三届“百越杯”福建省高校网络空间安全大赛-Do you know upload?

    进去提示有提示文件包含漏洞 拿到源码发现这里上传验证只有MIME验证 可直接抓包改 image/gif 绕过 接下来就是这次学到的点了 菜刀连接过后怎么都找不到flag文件,但是这里找到了数据库配置文 ...