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]

转载于:https://www.cnblogs.com/feiling/p/3228736.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值