LeetCode OJ: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,23,2,1 → 1,2,31,1,5 → 1,5,1
求下一排列,由于递减序列就没有下一排列了,那么从右往左找第一个递减序列(就是从左向右的递增的),找到这两个数之后,从右往左找第一个大于这个数的数,交换这两个数之后,再将原来的递减序列(从右向左递增)reverse之后就得到下一排列了。这题本来没写出来,看了别人的实现,代码如下所示:
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int sz = nums.size();
int i;
for(i = sz - ; i > ; --i){
if(nums[i] > nums[i - ])
break;
}
if(i > ){
i--;
int j = sz - ;
while(nums[j] <= nums[i]) j--;
swap(nums[i], nums[j]);
reverse(nums.begin() + i + , nums.end());
}else
reverse(nums.begin(), nums.end());
}
};
LeetCode OJ:Next Permutation(下一排列)的更多相关文章
- [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 ...
- [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 ...
- 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] ...
- Leetcode题库——31.下一个排列
@author: ZZQ @software: PyCharm @file: nextPermutation.py @time: 2018/11/12 15:32 要求: 实现获取下一个排列的函数,算 ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
- LeetCode OJ 60. Permutation Sequence
题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...
随机推荐
- java 遍历map的方法
package com.jackey.topic; import java.util.ArrayList;import java.util.HashMap;import java.util.Itera ...
- HDU1081:To The Max(最大子矩阵,线性DP)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). ...
- sql join on 与where
转载:http://www.cnblogs.com/Jessy/p/3525419.html left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录. right join : ...
- Python(装饰器)
eval 内容从字符串中提取出来,用Python运行一遍__name__得到函数名字 闭包函数 定义: 函数内部定义的函数称为内部函数 该内部函数包含对外部作用域,而不是对全局作用域的名字的引用 那么 ...
- SqlServer分页总结-摘抄
sqlserver2008不支持关键字limit ,所以它的分页sql查询语句将不能用mysql的方式进行,幸好sqlserver2008提供了top,rownumber等关键字,这样就能通过这几个关 ...
- WCF经典代码
Array.CreateInstance(typeof(object), methodCall.Args.Length) 1. DataContractSerializer支持的类型......... ...
- cdoj1338郭大侠与英雄学院
地址:http://acm.uestc.edu.cn/#/problem/show/1338 思路: 郭大侠与英雄学院 Time Limit: 6000/2000MS (Java/Others) ...
- 【持续更新】ultraedit工具使用总结
常用设置及快捷键 设置Ultraedit自动换行: 永久修改: 点击菜单栏的“高级→配置”,找到“编辑器→自动换行/制表符设置”,选择“默认为每个文件启用自动换行”,ok. 临时修改: 快捷键 Ctr ...
- $python正则表达式系列(6)——"或"表达式的用法
import re s1 = u'距离地铁5号线189米' s2 = u'距离地铁5号线(环中线)189米' s3 = u'距离地铁5号线(环中线)189米' p1 = re.compile(u'号线 ...
- ubuntu16.04 虚拟机 安装win7/win10
http://www.xitongcheng.com/jiaocheng/xtazjc_article_26588.html https://blog.csdn.net/sunyao_123/arti ...