27.Next Permutation(下一个字典序列)
Level:
Medium
题目描述:
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,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
思路分析:
这道题要求是给出当前序列的下一个序列(当前序列的下一个更大的序列),意思就是求当前序列的下一个字典序列。那么我们有以下算法:
给定一个序列假如是a1,a2,a3,..ai,ai+1,ai+2..,aj..an
1.找到最后一个正序序列ai,ai+1;
2.找到ai后面最后一个比他大的数aj;
3.交换ai和aj; a1,a2,a3,..aj,ai+1,ai+2..,ai..an
4.将aj后面的所有数反转,即得到下一个序列,即下一个比它大的数
代码:
public class Solution{
public void nextPermutation(int []nums){
if(nums==null||nums.length==0)
return;
//第一步找到最后一对正序序列
int flag=-1;
for(int i=nums.length-1;i>=1;i--){
if(nums[i]>nums[i-1]){
flag=i-1;
break;
}
}
// 如果不存在正序,则证明该序列是由大到小的逆序,则反转整个序列
if(flag==-1){
reverse(nums,flag+1,nums.length-1);
return;
}
//如果存在正序,则找到flag后面最后一个比它大的数,并交换
for(int j=nums.length-1;j>flag;j--){
if(nums[j]>nums[flag]){
int temp=nums[j];
nums[j]=nums[flag];
nums[flag]=temp;
break;
}
}
//反转flag位置后的序列
reverse(nums,flag+1,nums.length-1);
return ;
}
public void reverse(int []nums,int start,int end){//反转序列
while(start<end){
int temp=nums[end];
nums[end]=nums[start];
nums[start]=temp;
start++;
end--;
}
}
}
27.Next Permutation(下一个字典序列)的更多相关文章
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- lintcode:next permutation下一个排列
题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...
- 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. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 2.1.12 Next Permutation 下一个字典序数组
对当前排列从后向前扫描,找到一对为升序的相邻元素,记为i和j(i < j).如果不存在这样一对为升序的相邻元素,则所有排列均已找到,算法结束:否则,重新对当前排列从后向前扫描,找到第一个大于i的 ...
- 031 Next Permutation 下一个排列
实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...
- 31. Next Permutation 返回下一个pumutation序列
[抄题]: Implement next permutation, which rearranges numbers into the lexicographically next greater p ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
随机推荐
- 10-stack
c++ stl栈stack介绍 C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构. c++ stl栈stack的头文件 ...
- 使用Qt Installer Framework制作软件安装包
概述 Qt Installer Framework(缩写QIF)是Qt官方用于生成软件安装包的工具.包括Qt Creator和Qt Installer Framework自身的安装包都是由这个工具制作 ...
- Redis学习(1)——下载与配置[转]
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主 ...
- Prolific PL2303 usb 转串口Win8 Win8.1驱动
买了根USB转RS232串口的线,Pl2303芯片的.卖家和官方都称不支持Win8,但鄙人不信在Win7上能用在Win8/8.1就用不起来. 官方最新版的v1.9.0的驱动描述说不支持Win 8/8. ...
- 开源SLAM
GitHub 上优秀的开源SLAM repo (更新中):https://www.jianshu.com/p/464ca0d0c254 当前的开源SLAM方案:https://www.cnblogs. ...
- mysql转ElasticSearch的分析 及JAVA API 初探
前言 最近工作中在进行一些技术优化,为了减少对数据库的压力,对于只读操作,在程序与db之间加了一层-ElasticSearch.具体实现是db与es通过bin-log进行同步,保证数据一致性,代码调用 ...
- yum使用,使用rpm指令安装rpm,使用dpkg指令安装deb
yum安装时如果报错提示安装失败,缺少库文件,可以使用: yum whatprovides 库名 之后安装提供的程序 yum remove xxx卸载 yum update 更新 解决 Require ...
- Android-ListView-SimpleCursorAdapter
在上篇博客,Android-ListView-SimpleAdapter,中介绍了SimpleAdapter的使用操作(SimpleAdapter面向的数据是非Cursor数据),而SimpleCur ...
- Mathcad操作tips:算式输入、变量定义与计算
算式输入 1. 数字与符号相乘,输入时不必手动输入乘号(“*”). 2. 以下有助于算式的可视化:a. 使用Math工具栏输入,并合理使用tab键:b. 合理使用空格键. 3. 输入开根号时,可用快捷 ...
- nowcoder(牛客网)提高组模拟赛第一场 解题报告
T1 中位数(二分) 这个题是一个二分(听说是上周atcoder beginner contest的D题???) 我们可以开一个数组b存a,sort然后二分b进行check(从后往前直接遍历check ...