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文件转成jar包再转成exe文件——图文教程
			
[本文简介] 由于课程设计的原因,研究着如何把java 程序转成exe,最终成功了,现在把过程记录分享一下. 本文将介绍如何把一个跑在eclipse的java应用,导出成jar文件,再变成exe可执行 ...
 - Android学习十二---在android上实现图像匹配
			
一.效果图及功能描述 效果图 点击ShowImg后 点击match,然后点击showmatch,可以不断点击showmatch. 主要功能描述:显示在SD卡上已经存在的图片test.jpg,根据图片在 ...
 - wtforms Form实例化流程(源码解析)
			
class LoginForm(Form): #首先执行后得到的结果是UnboundField()对象 name=simple.StringField( label='用户名', validato ...
 - poj3903 Stock Exchange 二分+dp
			
题目地址:http://poj.org/problem?id=3903 题目: Description The world financial crisis is quite a subject. S ...
 - CF932E Team Work(第二类斯特林数)
			
题目 CF932E Team Work 前置:斯特林数\(\Longrightarrow\)点这里 做法 \[\begin{aligned}\\ &\sum\limits_{i=1}^n C_ ...
 - FIFO设计验证经验谈
			
概述: FIFO是电路设计中非常重要的一个基本电路.一般的超大规模集成电路中,都会用到FIFO.所以,FIFO是每个SOC设计和验证工程师必须掌握的一种核心电路. FIFO电路又分为异步FIFO和同步 ...
 - JavaWeb Filter
			
1. 过滤器概述 1.1. 什么是过滤器 Filter译为过滤器,是JavaWeb的三大组件之一,用于在Servlet之外对Request或者Response进行修改.对于Web应用程序来说,过滤器是 ...
 - QML、Qt Quick
			
当用widget开发Qt时, 语言:C++ 库:Qt库 当用QML开发时, 语言:QML 库:Qt Quick
 - NOIP 数字游戏
			
描述 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易.游戏是这样的,在你面前有一圈整数(一共n个),你要按顺序将其分为m ...
 - 移植madplay到jz2440【学习笔记】
			
平台:jz2440 作者:庄泽彬(欢迎转载,请注明作者) 说明:韦东山一期视频学习笔记 交叉编译工具:arm-linux-gcc (GCC) 3.4.5 PC环境:ubuntu16.04 一.移植ma ...