Next Permutation 求下一个排序@LeetCode
2013-11-21 13:35
274 查看
完全没有思路,参考了 http://www.cnblogs.com/etcow/archive/2012/10/02/2710083.html
分三步: 1. 从后往前找falling edge,下降沿。(下降之后的那个元素) 2. 从下降沿开始往后找出替换它的元素。(就是第一个比它小的前一个元素) 3. 反转后面所有元素,让他从小到大sorted(因为之前是从大到小sorted的) 例如 “547532“ 1. “547532”, 4是下降沿。 2. “547532”, 5是要替换的元素, 替换后得到 “ 557432” 3. "557432", 7432反转,得到 “552347”。
package Level4;
/**
* 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
*
*/
public class S31 {
public static void main(String[] args) {
}
public void nextPermutation(int[] num) {
if(num.length <= 1){
return;
}
// 1. 从后往前找falling edge,下降沿。(下降之后的那个元素)
int edge = -1;
for(int i=num.length-2; i>=0; i--){
if(num[i] < num[i+1]){
edge = i;
break;
}
}
if(edge > -1){
// 2. 从下降沿开始往后找出替换它的元素。(就是第一个比它小的前一个元素)
for(int i=edge+1; i<num.length; i++){
if(num[edge] >= num[i]){
nextPermutationSwap(num, edge, i-1);
break;
}
if(i == num.length-1){
nextPermutationSwap(num, edge, i);
break;
}
}
}
// 3. 反转后面所有元素,让他从小到大sorted(因为之前是从大到小sorted的)
for(int i=edge+1, j=num.length-1; i<=edge+(num.length-edge-1)/2; i++, j--){
nextPermutationSwap(num, i, j);
}
}
public void nextPermutationSwap(int[] num, int i, int j){
int tmp = num[i];
num[i] = num[j];
num[j] = tmp;
}
}
分三步: 1. 从后往前找falling edge,下降沿。(下降之后的那个元素) 2. 从下降沿开始往后找出替换它的元素。(就是第一个比它小的前一个元素) 3. 反转后面所有元素,让他从小到大sorted(因为之前是从大到小sorted的) 例如 “547532“ 1. “547532”, 4是下降沿。 2. “547532”, 5是要替换的元素, 替换后得到 “ 557432” 3. "557432", 7432反转,得到 “552347”。
package Level4;
/**
* 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
*
*/
public class S31 {
public static void main(String[] args) {
}
public void nextPermutation(int[] num) {
if(num.length <= 1){
return;
}
// 1. 从后往前找falling edge,下降沿。(下降之后的那个元素)
int edge = -1;
for(int i=num.length-2; i>=0; i--){
if(num[i] < num[i+1]){
edge = i;
break;
}
}
if(edge > -1){
// 2. 从下降沿开始往后找出替换它的元素。(就是第一个比它小的前一个元素)
for(int i=edge+1; i<num.length; i++){
if(num[edge] >= num[i]){
nextPermutationSwap(num, edge, i-1);
break;
}
if(i == num.length-1){
nextPermutationSwap(num, edge, i);
break;
}
}
}
// 3. 反转后面所有元素,让他从小到大sorted(因为之前是从大到小sorted的)
for(int i=edge+1, j=num.length-1; i<=edge+(num.length-edge-1)/2; i++, j--){
nextPermutationSwap(num, i, j);
}
}
public void nextPermutationSwap(int[] num, int i, int j){
int tmp = num[i];
num[i] = num[j];
num[j] = tmp;
}
}
相关文章推荐
- LeetCode-Next Permutation-下一个排列-扫描+排序(不是最优解)
- 【leetcode】寻找两个已排序数组的中位数(类似二分)
- 排序相关—— 相邻两数的最大差值( Maximum Gap-LeetCode)
- [Leetcode] Remove duplicates from sorted list 从已排序的链表中删除重复元素
- LeetCode 83. 删除排序链表中的重复元素
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
- sort(数组名+从该元素的下一个元素开始,数组名+要排序的最后一个元素(不是元素下标!),cmp)
- 【LeetCode】4. 两个排序数组的中位数 结题报告 (C++)
- 【LeetCode】Sort Colors 数组排序
- Leetcode26:Remove Duplicates from Sorted Array(移除已排序数组中重复的数字)
- [LeetCode] 681. Next Closest Time 下一个最近时间点
- [leetcode] 【排序】 147. Insertion Sort List
- LeetCode 31 Next Permutation (下一个排列)
- Leetcode 4 Median of Two Sorted Arrays 两排序数组的中位数
- LeetCode Merge Sorted Array(直接插入排序)
- leetcode之合并已经排序好的数组
- leetcode【第十三周】颜色排序
- [LeetCode] 83. Remove Duplicates from Sorted List 删除排序链表中的重复元素 @python
- [leetcode 75] Sort Colors-----------只有三种颜色的排序
- Leetcode 148 Sort List 单链表排序