[LeetCode] 31. Next Permutation 下一个排列
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,23,2,1 → 1,2,31,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
类似题目:
参考资料:
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 下一个排列的更多相关文章
- [leetcode]31. Next Permutation下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【LeetCode每天一题】Next Permutation(下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode(31): 下一个排列
Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...
- Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 031 Next Permutation 下一个排列
实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...
- lintcode:next permutation下一个排列
题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...
- 31. Next Permutation (下一个全排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- 题目:利用Calendar类计算自己的出生日期距今天多少天,再将自己的出生日期利用SimpleDateFormat类设定的格式输出显示
package cn.exercise; import java.util.Calendar; import java.util.Date; import java.text.SimpleDateFo ...
- 九、Spring之BeanFactory源码分析(一)
Spring之BeanFactory源码分析(一) 注意:该随笔内容完全引自https://blog.csdn.net/u014634338/article/details/82865644,写的 ...
- AutoDesk公司搞的fbx模型格式
FBX® data exchange technology is a 3D asset exchange format that facilitates higher-fidelity data ex ...
- 安卓微信对接H5微信支付出现“商家参数有误,请联系商家解决”的问题处理
最近遇到客户在对接我们微信支付的时候,一些商家反馈在用户支付的过程中会出现报错,出错的截图如下: 查看微信官方文档如下:https://pay.weixin.qq.com/wiki/doc/api/H ...
- 一次压测中tomcat生成session释放不及时导致的频繁fullgc性能优化案例
性能问题:老年代一直处于占满状态,为什么没有发生内存溢出 以HotSpot VM的分代式GC为例,普通对象分配都是在young gen进行的,具体是从在位于young gen中的eden space中 ...
- nodejs块级作用域
现在让我们了解3个关键字var.let.const,的特性和使用方法. var JavaScript中,我们通常说的作用域是函数作用域,使用var声明的变量,无论是在代码的哪个地方声明的,都会提升到当 ...
- Jmeter进阶技能-数据库信息,传递参数
因为项目的原因,假设我们要实现如下要求:从数据库的用户表里获取用户信息,并作为参数全部传递给登录请求,分别完成登录操作. 01Jmeter连接数据库 1.添加JDBC Connection Confi ...
- 【学习笔记】兄弟连LINUX视屏教程(沈超 李明)
发现自己的linux水平楞个瓜皮,找个视屏教程学习一哈 1 linux系统简介 1.1 UNIX和Linux发展史 unix发展历史:1969年,美国贝尔实验室的肯.汤普森开发出unix系统,1971 ...
- Git学习笔记3-远程仓库
1.添加远程仓库 $ git remote add [shortname] [url] $ git remote add origin https://github.com/Mike199201/Gi ...
- 渗透技巧——Windows系统的帐户隐藏
渗透技巧——Windows系统的帐户隐藏 2017-11-28-00:08:55 0x01 帐户隐藏的方法 该方法在网上已有相关资料,本节只做简单复现 测试系统:·Win7 x86/WinXP 1. ...