31. Next Permutation

[brainstorm]
1 find increase pair, swap the pair.
the left number should be as close to the right boundary as possible.
e.g.
2 3
2 0 0 3
2 5 4 3
6 8 9 7

2 see more cases by full permutation
e.g.
1 2 3
1 3 2
2 1 3
3 1 2
3 2 1
find some exceptional case,
1 3 2 —> 2 1 3
2 3 1 —>3 1 2
use 2 to replace 1, use 1 3 as minimum.
use bigger first num, use minimum permutation for the remaining.

3 another case
3 5 4 2 1 -> 4 5 3 2 1
as requirement, in place, just O(1) space, that means only swap.
we cannot keep remaining in structure, how to get minimum?
3 5 4 2 1 -> 4 1 2 3 5
however, just reverse the remaining do the trick.

[brainstorm]
1 when alog cover some cases, but still some exceptional case,
observer how far algo go for exceptional case
compare it to expected result, see how close
in the prob, just one reverse do the trick.

2 in place, as required, give me great confidence to start by swap.
do swap as small step and observer, see what next.
so called incremental progress.

3 for exceptional case, look 2 cases, try to find pattern.
1 3 2 —> 2 1 3
2 3 1 —>3 1 2

[draft]
在这里插入图片描述

[first bug]

class Solution {
    public void nextPermutation(int[] nums) {

        int n = nums.length;
        for(int i=n-2;i>=0;i--){
            for(int j=i+1;j<n;j++){
                if(nums[i]<nums[j]){
                    swap(nums, i, j);
                    return;
                }
            }
        }
        
        int l = 0;
        int r = n-1;
        while(l<r){
            swap(nums, l, r);
            l++;
            r--;
        }
    }

    void swap(int[] nums, int i, int j){

        //swap
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;        
    }

}

[second bug]

class Solution {
    public void nextPermutation(int[] nums) {

        int n = nums.length;
        for(int i=n-2;i>=0;i--){
            for(int j=i+1;j<n;j++){
                if(nums[i]<nums[j]){
                    swap(nums, i, j);
                    swap(nums, i+1, n-1);
                    return;
                }
            }
        }
        
        int l = 0;
        int r = n-1;
        while(l<r){
            swap(nums, l, r);
            l++;
            r--;
        }
    }

    void swap(int[] nums, int i, int j){

        //swap
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;        
    }

}

[third bug]

class Solution {
    public void nextPermutation(int[] nums) {

        int n = nums.length;
        for(int i=n-2;i>=0;i--){
            for(int j=i+1;j<n;j++){
                if(nums[i]<nums[j]){
                   for(j=i+1;j<n-1;j++){
                       if(nums[i]<nums[j] && nums[i]>nums[j+1]){
                           swap(nums, i, j);
                           rev(nums, i+1, n-1);
                           return;
                       }
                   }
                   swap(nums, i, n-1);
                   rev(nums, i+1, n-1);
                   return; 
                }
                
            }
        }

        int l=0;
        int r=n-1;
        rev(nums, l, r);        
    }

    void rev(int[] nums, int l, int r){
        while(l<r){
            swap(nums, l, r);
            l++;
            r--;
        }
    }

    void swap(int[] nums, int i, int j){

        //swap
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;        
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值