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 ...
随机推荐
- pcl point merge
http://pointclouds.org/documentation/tutorials/pairwise_incremental_registration.php#pairwise-increm ...
- HDU 5977 Garden of Eden (树分治+状态压缩)
题意:给一棵节点数为n,节点种类为k的无根树,问其中有多少种不同的简单路径,可以满足路径上经过所有k种类型的点? 析:对于路径,就是两类,第一种情况,就是跨过根结点,第二种是不跨过根结点,分别讨论就好 ...
- 三)EasyUI layout
参考文档 http://www.jeasyui.com/documentation/layout.php
- org.slf4j.impl.SimpleLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext
查看日志信息: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/app/a ...
- TextView 垂直居中
需要区分的是这里的top,bottom,ascent,descent,baseline是指字内容的属性,通过getPaint().getFontMetricsInt()来获取得到.和字体内容的外部容 ...
- ENVI 图像特征提取
- css基础 引用方式 标签选择器 优先级 各式布局
今天讲的css基础,了解了css即层叠式表,是美化网页,控制页面的样式. 样式表引进网页的3种方式1内联式,语法例子:<div style="width: 100px;height: ...
- SocketAsyncEventArgs
SocketAsyncEventArgs是.net提供的关于异步socket类,封装了IOCP的使用,可以用它方便的实现NIO(non-blocking IO) NIO对于提升某些场景下Server性 ...
- C++中的类型判断typeid()操作与java中的 instanceof 做比较
这是RTTI(运行阶段类型识别)的问题,c++有三个支持RTTI的元素: 1. dynamic_cast 操作符 如果可能的话,dynamic_cast操作符将使用一个指向基类的指针来生成一个 ...
- Opengl中的GLUT下的回调函数
void glutDisplayFunc(void (*func)(void)); 注册当前窗口的显示回调函数 参数: func:形为void func()的函数,完成具体的绘制操作 这个函数告诉GL ...