Next Permutation
来源:互联网 发布:相片排版软件 编辑:程序博客网 时间:2024/04/30 04:38
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
Analysis: Search from the end to the beginning of the array. Search for a position of i, for which we can find one or more elements in the sub array num[i+1 ... num.length-1]. Choose the smallest qualified number in num[i+1 ... num.length-1] which is greater than num[i] and then swap these two numbers in place. After that, sort the sub array num[i+1 ... num.length-1] in ascending order. If we cannot find the number to be swapped after iterating the entire array, such array must be in descending order.
public class Solution { public void nextPermutation(int[] num) { for(int i=num.length-2; i>=0; i--) { int minVal = Integer.MAX_VALUE; int minPosition = -1; for(int j=i+1; j<num.length; j++) { if(num[j]<minVal && num[j]>num[i]) { minVal = num[j]; minPosition = j; } } if(minPosition != -1) { // swap num[i] = num[i] - num[minPosition]; num[minPosition] = num[i] + num[minPosition]; num[i] = num[minPosition] - num[i]; // insertion sort the rest of the elements after i for(int q=i+2; q<num.length; q++) { int temp=num[q], k=q-1; while(num[k]>=temp && k>=i+1) { num[k+1] = num[k]; k--; } num[k+1] = temp; } return; } } // the original array is in descending order Arrays.sort(num); return; }}
Update: 30/01/2014
public class Solution { public void swap(int[] num, int i, int minPosition) { int temp = num[i]; num[i] = num[minPosition]; num[minPosition] = temp;return;}public void insertionSort(int[] num, int start) {for(int i=start+1; i<num.length; i++) {int temp=num[i], j=i-1;for( ; j>=start; j--) {if(num[j]>temp) num[j+1]=num[j];else break;}num[j+1] = temp;}return;}public void nextPermutation(int[] num) { for(int i=num.length-2; i>=0; i--) { int minVal = Integer.MAX_VALUE; int minPosition = -1; for(int j=i+1; j<num.length; j++) { if(num[j]<minVal && num[j]>num[i]) { minVal = num[j]; minPosition = j; } }if(minPosition != -1) {swap(num, i, minPosition); // swap such two numbersinsertionSort(num, i+1); // sort the digits after the swaped onereturn;}} // the original array is in descending order Arrays.sort(num); return; }}
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- WebRTC - Introduction to WebRTC architecture - 02
- hadoop三个配置文件的参数含义说明
- HDU 1395
- 动态规划——2 Warshall算法
- 《新理解矩阵2》:矩阵是什么?
- Next Permutation
- 计算1000!包含多少个0?
- 创建二叉树并遍历二叉树
- 奋斗历程-路漫长
- 视图控制器的视图的卸载过程
- UC优视创始人何小鹏:移动互联网不得不看的5个大坑
- 动态规划——3 floyd算法
- “iOS 模拟器”未能安装此应用程序的解决方法
- Oracle PlSQL常用命令