[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 ...
随机推荐
- 聊聊SOA面向服务架构
什么是SOA SOA(Service-Oriented Architecture),即面向服务的架构.SOA是一种粗粒度.松耦合服务架构,服务之间通过简单.精确定义接口进行通讯,不涉及底层编程接口和通 ...
- java的final用法
转自:http://blog.163.com/maomaoyu_1012/blog/static/19060130520116269329894/ 1. 修饰基础数据成员的final ...
- MYSQL中'TYPE=MyISAM'错误的解决方案
create 语句后面的TYPE=MyISAM TYPE=MyISAM 和 ENGINE=MyISAM 都是设置数据库存储引擎的语句 ,(老版本的MySQL使用TYPE而不是ENGINE(例如,TYP ...
- php 以图搜图
感知哈希算法count < =5 匹配最相似count > 10 两张不同的图片var_dump(ImageHash::run('1.jpg’, '2.jpg’)); <?php c ...
- UE对一个很长的字符按16位自动换行
① ②设定16个位 ③ ④结果
- jfreeChart柱状图各属性详细设置
一. 下载与环境配置 此最新版本为 1.0.13 解压jfreechart-1.0.13.zip 将lib目录下的jfreechart-1.0.13.jar .jcommon-1.0.16.jar 复 ...
- Arduino101学习笔记(十一)—— 蓝牙BLE
一.BLE技术简介 第四代蓝牙既包括传统的蓝牙,现在标有"蓝牙经典",和新的低功耗蓝牙(Bluetooth LE,或BLE).低数据速率,低功耗优化. 蓝牙LE广播就像一个社区公告 ...
- (转载)RTorrent 命令行使用说明
转自:http://blog.chinaunix.net/uid-22457844-id-2973262.html 参考:http://forum.ubuntu.org.cn/viewtopic.ph ...
- 词法分析 after Coding
学习词法分析,认为词法分析很难. 虽然不懂,但是要完成作业. 去图书馆或者看书借鉴代码,修改错误,让代码正常运行. 学习词法分析后,了解到自己有很多的不足: 1.代码不是很熟练,课本知识不了解.知识面 ...
- JavaScript案例一:Window弹窗案例
注:火狐可运行,谷歌不可运行(安全级别高) <!DOCTYPE html> <html> <head> <title>JavaScript 弹窗案例&l ...