[Leetcode] 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,2
3,2,1
→ 1,2,3
1,1,5
→ 1,5,1
Solution:
http://fisherlei.blogspot.com/2012/12/leetcode-next-permutation.html
算法思想如图所示:
代码如下:
public class Solution {
public void nextPermutation(int[] num) {
if(num.length<2)
return;
int N=num.length;
int index=N-1;
while(index>0){
if(num[index]<=num[index-1])
index--;
else
break;
}
if(index==0){
Arrays.sort(num);
return;
} int val=num[index-1];
//System.out.println(val);
int j=N-1;
while(j>index-1){
if(num[j]>val){
break;
}
else
j--;
}
// System.out.println(num[j]);
int temp=val;
num[index-1]=num[j];
num[j]=temp;
Arrays.sort(num, index, N);
}
}
[Leetcode] Next Permutation的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- LeetCode Palindrome Permutation
原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Find Permutation 找全排列
By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...
- [leetcode]Next Permutation @ Python
原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...
- LeetCode Find Permutation
原题链接在这里:https://leetcode.com/problems/find-permutation/description/ 题目: By now, you are given a secr ...
随机推荐
- <转>ORA-06413 连接未打开错误
ORA-06413 Connection not open.Cause: Unable to establish connection.Action: Use diagnostic procedure ...
- go-martini 简单分析之二
martini.go 对路由采用正则表达式处理,最终转化成正则表达式. 添加route对应的调用栈 按照生成,验证,添加的步骤 route := newRoute(method, pattern, h ...
- 手机web页面制作时的注意事项
一.手机页面的标准头规范 字符编码使用utf-:指定页面手机内存缓存中的存储时间段 device-width:通知浏览器使用设备的宽度作为可视区的宽度 initial-scale - 初始的缩放比例 ...
- hdu 4734 数位dp
给一个数A (十进制表示形式为AnAn-1An-2 ... A2A1,定义函数 F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1,给一个B, ...
- 直传文件到Azure Storage的Blob服务中
(此文章同时发表在本人微信公众号“dotNET每日精华文章”,欢迎右边二维码来关注.) 题记:为了庆祝获得微信公众号赞赏功能,忙里抽闲分享一下最近工作的一点心得:如何直接从浏览器中上传文件到Azure ...
- linux c学习笔记----互斥锁属性
转自:http://lobert.iteye.com/blog/1762844 互斥锁属性 使用互斥锁(互斥)可以使线程按顺序执行.通常,互斥锁通过确保一次只有一个线程执行代码的临界段来同步多个线程. ...
- servlet、genericservlet、httpservlet之间的区别
转自:http://blog.csdn.net/rat9912345/article/details/5161789 当编写一个servlet时,必须直接或间接实现servlet接口,最可能实现的方法 ...
- 咱就入个门之NHibernate映射文件配置(二)
上一篇主要介绍了NHibernate映射文件的基础配置,这篇我们介绍下NHibernate的一对多及多对一配置(文中我直接使用双向关联,即一和多两端都配置,开发中可以只使用一端),同时略带介绍下NHi ...
- jQuery Mobile Datepicker 使用
插件引入文件: <meta name="viewport" content="width=device-width, initial-scale=1"&g ...
- Android 开发工具之Codota
Codota 的特性 的确,正如codota 官方所说,的确是精品,他的搜索源,不仅只有Github,而且还有知名博客和开发者网站,让你搜索一个东西,不用在找上半天: 除了搜索功能,首页的下方还罗列比 ...