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 ...
随机推荐
- spring4-3-AOP-面向切面编程
AOP常用的两个用户:日志和验证.也就是程序追踪和数据验证. 直接使用代码实现,距离如下:
- udacity term_sim.x86_64 ubuntu16.04 Vmware
打印信息 ./term2_sim.x86_64 Set current directory to /home/mwolfram/udacity/sdcnd/term2/term2_sim_linux ...
- Anaconda 安装和配置
Anaconda 安装和配置 1. Anaconda 安装 Anaconda说明及安装过程:Anaconda详细安装使用教程 2. Anaconda和Pip源修改 Anaconda源修改:打开Anac ...
- 爬虫框架scrapy的基本内容
Scrapy介绍 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以帮助用户简单快速的部署一个专业的网络爬虫.如果说前面我们写的定制bs4爬虫是”手动挡“,那Scrapy就相当 ...
- css确定取消 悬浮底部样式 和 金额 后缀
.blockquote-bottom { width: 100%; position: fixed; margin: 0; bottom: 0; left: 0; text-align: center ...
- SAC学习笔记(一)——SAC安装
1.软件包申请 地址: http://www.iris.edu/forms/sac_request.htm 其中的表单需要正确认真填写,选择你所需要的系统平台Linux.Mac OS.Sol ...
- NBA常识 位置的划分 足球:越位等于抢跑
篮球:1号位——组织后卫(控球,组织)2号位——得分后卫(中远投篮,突破)3号位-----小前锋(突破,中远投篮)4号位——大前锋(二中锋,篮板,背身单打,禁区防守)5号位——中锋(篮板.背身单打,禁 ...
- 15 输入三个整数x,y,z,请把这三个数由小到大输出。
题目:输入三个整数x,y,z,请把这三个数由小到大输出. public class _015ThreeNumberSort { public static void main(String[] arg ...
- Linux查询系统信息命令
Linux查看系统信息是比较基础的知识,所以这个应该都需要掌握,命令和解释如下: #uname -a 查看操作系统.内核.CPU信息 #head -n 1 /etc/issue ...
- Lua的文件IO操作
Lua 标准库 - 输入输出处理(input and output facilities) 转载于:http://blog.csdn.net/duanxuyun 文本Tag: Lua [IT168 技 ...