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 ...
随机推荐
- ie8不兼容input的placeholder属性但是要实现其效果的方法
通过学习前辈的思想,个人想法整理如下: 通过两个元素标签,仿造出placeholder的内容 使用position定位好两个元素标签 第一要隐藏两个元素标签display:none,在<!--[ ...
- java.lang.NoClassDefFoundError: Could not initialize class net.sf.json.util.JSONUtils
页面报错: root: java.lang.NoClassDefFoundError: Could not initialize class net.sf.json.util.JSONUtils 出错 ...
- memcached-session-manager的一些理解
1.节点分配 粘性规划: Tomcat-1(t1)将session优先备份在运行在令一台机器上的memcached-2(m2)上面,仅当m2失效的时候,t1才会将sessin存储在m1上面(m1是t1 ...
- PHP性能如何实现全面优化?
性能是网站运行是否良好的关键因素, 网站的性能与效率影响着公司的运营成本及长远发展,编写出高质高效的代码是我们每个开发人员必备的素质,也是我们良好的职业素养. 如何优化PHP性能呢? 一.变量(重要) ...
- poj1936--暴力解法
求s1是否是s2的子串. 分析: 例如sequence 和 subsequence . 从头开始比较,s1[0]与s2[0]相同,那么它们下标都可+1,接着比较s1[1]和s2[1],不相同:这时应该 ...
- leetcode first bad version python
# The isBadVersion API is already defined for you. # @param version, an integer # @return a bool # d ...
- wdcp升级php和mysql
安装没什么好说的,按照wdcp官方去搞就行了,这里如果是centos系统建议使用rpm安装方式附件如下(这里包含了本文后面用到的三个文件) http://download.csdn.net/detai ...
- 环境配置与JBoss安装-EJB3.0入门经典学习笔记(1)
目录 1. JDK的安装 2. JBoss的安装 3. JBoss安装目录说明 1. JDK的安装 1) 下载JDK 下载地址:http://www.oracle.com/technetwork/ja ...
- background:url 的使用方法
#pingfen li{ width:27px; float:left; height:28px; cursor:pointer; background:url( ; list-style:none; ...
- php面向对象编程学习之高级特性
前几天写了一篇关于php面向对象基础知识的博客,这两天看了php面向对象的高级特性,写出来记录一下吧,方便以后拿出来复习. 面向对象除了最基本的定义类之外,最主要就是因为面向的一些高级特性,运用这些高 ...