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

 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 len = num.length;
 6         int vioIndex = len - 1;
 7         while(vioIndex > 0){
 8             if(num[vioIndex - 1] < num[vioIndex]){
 9                 break;
10             }
11             vioIndex --;
12         }
13         
14         if(vioIndex > 0){
15             vioIndex --;
16             int right = len - 1;
17             while(right >= 0 && num[vioIndex] >= num[right]){
18                 right --;
19             }
20             
21             int tmp = num[vioIndex];
22             num[vioIndex] = num[right];
23             num[right] = tmp;
24             vioIndex ++;
25         }
26         
27         int end = len - 1;
28         while(vioIndex < end){
29             int tmp = num[vioIndex];
30             num[vioIndex] = num[end];
31             num[end] = tmp;
32             vioIndex ++;
33             end --;
34         }
35         
36         
37     }
38 }

 

数学太烂!!!

http://fisherlei.blogspot.com/2012/12/leetcode-next-permutation.html

 refactor code:

 1 public class Solution {
 2     public void nextPermutation(int[] num) {
 3         // Note: The Solution object is instantiated only once and is reused by each test case.
 4         int length = num.length;
 5         int index = - 1;
 6         for(int i = length - 1; i >= 1; i--){
 7             if(num[i] > num[i - 1]){
 8                 index = i;
 9                 break;
10             }
11         }
12         
13         if(index == -1){
14             reverseArray(num, 0, length - 1);
15         } else {
16             int biggerIndex = findBig(num[index - 1], index, num);
17             swap(num, biggerIndex, index - 1);
18             reverseArray(num, index, length - 1);
19         }
20     }
21     
22     public int findBig(int sentinal, int index, int[] num){
23         int bigIndex = index;
24         int bigValue = num[index];
25         for(int i = index + 1; i < num.length; i++){
26             if(num[i] > num[index - 1] && num[i] <= bigValue){
27                 bigValue = num[i];
28                 bigIndex = i;
29             }
30         }
31         return bigIndex;
32     }
33     
34     public void reverseArray(int[] num, int start, int end){
35         while(start < end){
36             swap(num, start, end);
37             start ++;
38             end --;
39         }
40     }
41     
42     public void swap(int[] num, int start, int end){
43         int tmp = num[start];
44         num[start] = num[end];
45         num[end] = tmp;
46     }
47 }

 

 重写代码过程中出现的错误:

1.line:18 反转从partition之后的数字(partition number is inclusive)

2.line:26 找到比sentinel大的最右侧的数字下标,因为最后需要反转,得到的结果要是最小的,看下例

Input:[2,3,1,3,3]

Output:[2,3,3,3,1]

Expected:[2,3,3,1,3]

posted @ 2013-07-31 20:40  feiling  阅读(493)  评论(0编辑  收藏  举报