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

时间:2021-09-08 23:45:58

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. &lbrack;leetcode&rsqb;31&period; Next Permutation下一个排列

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

  2. leetCode 31&period;Next Permutation &lpar;下一个字典序排序&rpar; 解题思路和方法

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

  3. &lbrack;LeetCode&rsqb; Next Permutation 下一个排列

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

  4. 【LeetCode每天一题】Next Permutation&lpar;下一个排列&rpar;

    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&period; Next Permutation &lpar;下一个全排列&rpar;

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

随机推荐

  1. 编写高性能SQL

    前言:系统优化中一个很重要的方面就是SQL语句的优化.对于海量数据,劣质SQL语句和优质SQL语句之间的速度差别可达到上百倍,可见对于一个系统不是简单的能实现其功能就可以了,而是要写出高质量的SQL语 ...

  2. android自定义控件实现TextView按下后字体颜色改变

    今天跟大家分享一下Android自定义控件入门,先介绍一个简单的效果TextView,按下改变字体颜色,后期慢慢扩展更强大的功能 直接看图片             第一张是按下后截的图,功能很简单, ...

  3. 查看Linux系统的版本以及位数

    1.查看版本 http://jingyan.baidu.com/article/215817f7e360bd1edb142362.html[root@localhost usr]# lsb_relea ...

  4. matlab的legend用法

    用Matlab画图时,有时候需要对各种图标进行标注,例如,用“+”代表A的运动情况,“*”代表B的运动情况. legend函数的基本用法是: LEGEND(string1,string2,string ...

  5. 20155205 郝博雅 Exp6 信息搜集与漏洞扫描

    20155205 郝博雅 Exp6 信息搜集与漏洞扫描 一.实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务 ...

  6. mysql解决数据库高并发

    分表分库 数据库索引 redis缓存数据库 读写分离 负载均衡: 将大量的并发请求分担到多个处理节点,由于单个处理节点的故障不影响服务,负载均衡集群同事也实现了高可用性.

  7. 七:python 对象类型详解三:列表

    一:列表简介: 1,列表可以包含任何种类的对象:数字.字符串甚至集合对象类型.列表都是可变对象,它们都支持在原处修改的操作,可以通过指定的偏移量和分片.列表方法调用.删除语句等方法来实现.关键的作用有 ...

  8. python3之paramiko模块

    1.paramiko模块介绍 paramiko模块提供了基于ssh连接,进行远程登录服务器执行命令和上传下载文件的功能.这是一个第三方的软件包,使用之前需要安装. 2.paramiko的使用方法 (1 ...

  9. C&num; TextWriter类

    来自:https://www.yiibai.com/csharp/c-sharp-textwriter.html C# TextWriter类是一个抽象类.它用于将文本或连续的字符串写入文件.它在Sy ...

  10. php面向对象的简单总结 &dollar;this &dollar;parent self

    面向对象涉及到的比较多,大概总结整理一下php的属性.对象,以及访问方式$this  $parent  self  的使用场景. 1. PHP类属性定义和访问方式: 1 <?php 2 clas ...