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

这一题的时间复杂度是O(n)。 解题思路:

从后往前找,m,j,i,l, , 如果num[i] > num[i + 1], 即 一直是倒序,则继续往前找, 直到找个一个num[i]< num[i + 1]的, 即第一个比它右边小的数字。 

这是因为如果 从后往前一直是倒序,则已经是permutation了 不可能出现更大的, 只有顺序反了的那个地方才是需要动的地方。

接着将这个数字num[i] , 和它右边的数字比较,找个一个刚好比他大一点的数字, 两者替换位置。 接着再将i 之后的数字排序,正序,即可。

 1 public class Solution {
 2     public void nextPermutation(int[] num) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         int j,i;
 6         for(i = num.length - 1; i > 0; i --){
 7             j = i - 1;
 8             if(num[j] < num[i]){
 9                 int ex = 0;
10                 int a;
11                 for(a = i; a < num.length; a ++){
12                     if(num[a] > num[j]){
13                         ex = num[a];
14                     }
15                     else{
16                         break;
17                     }
18                 }
19                 num[a - 1] = num[j];
20                 num[j] = ex;
21                 for(int ii = i; ii < (num.length - i) / 2 + i; ii ++){
22                     int cur = num[ii];
23                     num[ii] = num[num.length - ii - 1+ i];
24                     num[num.length - ii- 1 + i] = cur;
25                 }
26                 return;
27             }
28         }
29         for(i = 0; i < num.length / 2; i ++){
30             int cur = num[i];
31             num[i] = num[num.length - 1 - i];
32             num[num.length - 1 - i] = cur;
33         }
34     }
35 }

 

 第三遍:

 1 public class Solution {
 2     public void nextPermutation(int[] num) {
 3         if(num == null || num.length < 2) return;
 4         int len = num.length;
 5         int point = -1;
 6         for(point = len - 2; point > -1; point --){
 7             if(num[point] < num[point + 1]) break;
 8         }
 9         if(point == -1){
10             for(int i = 0; i < num.length / 2; i ++){
11                 int cur = num[i];
12                 num[i] = num[num.length - 1 - i];
13                 num[num.length - 1 - i] = cur;
14             }
15             return;
16         }
17         int pos = 0;
18         for(int i = point + 1; i < len; i ++){
19             if(num[i] > num[point]) pos = i;
20         }
21         int ttmp = num[point];
22         num[point] = num[pos];
23         num[pos] = ttmp; 
24         
25         for(int i = 1; i <= (len - point - 1) / 2; i ++){
26             int tmp = num[point + i];
27             num[point + i] = num[len - i];
28             num[len - i] = tmp;
29         }
30     }
31 }

 

posted on 2013-09-11 01:41  Step-BY-Step  阅读(395)  评论(0编辑  收藏  举报

导航