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, 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,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1
字典序:
对于数字1、2、3......n的排列,不同排列的先后关系是从左到右逐个比较对应的数字的先后来决定的。例如对于5个数字的排列 12354和12345,排列12345在前,排列12354在后。按照这样的规定,5个数字的所有的排列中最前面的是12345,最后面的是 54321。
那么1234的全排列从小到大的顺序也就是字典序的顺序,依次如下:
1234,1243,1324,1342,1423,1432,2134,2143,2314,2341,2413,2431,3124,3142,3214,3241,3412,3421,4123,4132,4213,4231,4312,4321
产生字典序下一个的非递归算法:
设P是[1,n]的一个全排列,P=P1P2...Pn=P1P2...Pj-1PjPj+1...Pk-1PkPk+1...Pn
寻找j=max{i|Pi<Pi+1},k=max{i|Pi>Pj},swap Pj和Pk,reverse Pj+1...Pn,得到的P‘=P1P2...Pj-1PkPn...Pk+1PjPk-1...Pj+1,即为P的字典序的下一个排列,当然这里指的是等长度的。
代码:
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int j=-,k=,temp,left,right;
//j=max{i|pi<pi+1}
for( int i=nums.size()-;i>=;i-- ){
if( nums[i]<nums[i+] ){
j=i;break;
}
}
if( j==- ){//3 2 1
sort(nums.begin(),nums.end());
}else{
//k=max{i|pi>pj}
for( int i=nums.size()-;i>=;i-- ){
if( nums[i]>nums[j] ){
k=i;break;
}
}
//swap pj pk
temp=nums[j];nums[j]=nums[k];nums[k]=temp;
//reverse pj+1 pn
left=j+;right=nums.size()-;
while( left<right ){
temp=nums[left];nums[left]=nums[right];nums[right]=temp;
left++;right--;
}
}
}
};
leetcode 31. Next Permutation(字典序的下一个)的更多相关文章
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- [LeetCode] 556. Next Greater Element III 下一个较大的元素 III
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- SGU 179 Brackets light(生成字典序的下一个序列)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=179 解题报告:输入一个合法的括号串,求出这个括号串的字典序的下一个串.(认为'(' ...
- [LeetCode] 496. Next Greater Element I 下一个较大的元素 I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- [LeetCode] 503. Next Greater Element II 下一个较大的元素 II
Given a circular array (the next element of the last element is the first element of the array), pri ...
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode 31. Next Permutation (下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- ASP.NET仿新浪微博下拉加载更多数据瀑布流效果
闲来无事,琢磨着写点东西.貌似页面下拉加载数据,瀑布流的效果很火,各个网站都能见到各式各样的展示效果,原理大同小异.于是乎,决定自己写一写这个效果,希望能给比我还菜的菜鸟们一点参考价值. 在开始之前, ...
- C# 调用其他的动态库开发应注意的问题
1.背景 程序开发语言可以说是五花八门,这就引出了一个新问题 ,不同语言开发的系统进行对接时相关调用的问题. 下面我主要说一下我自己在做接口开发时遇到的问题及解决方法仅供参考,我使用的C#开发进行对接 ...
- git本地仓库与github远程仓库链接协议问题
前提条件:有github账号,本地安装了git,能上网. 环境:ubuntu14.0.4LTS 首先在你得在github上创建一个仓库new repository,然后再本地创建一个文件夹mkdir ...
- CentOS 配置防火墙操作实例(启、停、开、闭端口)CentOS Linux-FTP/对外开放端口(接口)TomCat相关
链接地址:http://blog.csdn.net/jemlee2002/article/details/7042991 CentOS 配置防火墙操作实例(启.停.开.闭端口): 注:防火墙的基本操作 ...
- sql 设计反模式
---恢复内容开始--- 1.乱穿马路 ---- > 目标 : 存储多值属性. 1) 错误方法: 使用格式化的逗号分割列表. 1-> 不适合查询,定位数据,无法运用聚合函数进行分组,不利于 ...
- 字节数转换为b,kb,mb,gb的方法 通用的手机流量计算方法
//通用的手机流量计算方法 private String byteToMB(long size){ long kb = 1024; long mb = kb*1024; long gb = mb*10 ...
- Apache新版配置虚拟主机的注意事项
1.关于没有默认索引文件(index.php或者index.html)时,列出目录:需要开启模块 LoadModule autoindex_module modules/mod_autoindex.s ...
- MongoDB(二)
通过程序来操作数据库: //链接字符串 string connectionString = "mongodb://192.168.1.107"; //数据库名 string dat ...
- FAQ: Python中if __name__ == '__main__':作用
#hello.pydef sayHello(): str="hello" print(str); if __name__ == "__main__": prin ...
- KPI考核
编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! KPI考核,Key Performance Indicator的缩写,指的是关键绩效指标考核法. 中文名 KPI考核 外 ...