LeetCode 刷题记录31. 31. 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 and use only constant 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. 首先从后往前看,找到开始变小的那个数,在本例中是2
    在这里插入图片描述
    如果一直是递增的,如 6,5,4,3,2,1说明已经是最后一个数列了,不用进行第二步

  2. 从后往前找第一个比2大的数,此例中是4,交换他们的位置
    在这里插入图片描述
    在这里插入图片描述

  3. 将第一步找到的位置后的数进行反转,即4后的数进行反转,如果是最后一个排列,则在第一步中的位置是-1,则整个数组进行翻转
    在这里插入图片描述
    在这里插入图片描述

C++:
注意两个while语句都带等号,即第一步严格找开始变小的,第二步严格找比第一步大的

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int n = nums.size();
        if(n <= 1) return;
        int i = n - 2;
        int j = n - 1;
        while(i >= 0 && nums[i] >= nums[i + 1]) --i;
        if(i >= 0){
            while(j >= 0 && nums[j] <= nums[i]) --j;
            swap(nums[i] , nums[j]);
        }
        reverse(nums.begin() + i + 1 , nums.end());
    }
};

java:
java中是值传递,所以swap函数不能像c++那么写
Swap in JAVA, 不是想象中的简单

class Solution {
    public void nextPermutation(int[] nums) {
        int n = nums.length;
        if(nums == null || n <= 1) return;
        int i = n - 2;
        int j = n - 1;
        while(i >= 0 && nums[i] >= nums[i + 1]) --i;
        if(i >= 0){
            while(j >= 0 && nums[j] <= nums[i]) --j;
            swap(nums, i, j);
        }
        reverse(nums, i + 1 , n - 1);
    }
    private void reverse(int[] nums, int start, int end){
        int i = start;
        int j = end;
        while(i < j){
            swap(nums, i, j);
            i++;
            j--;
        }
    }
    private void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

python:
python中可以采用元组的方式进行交换

class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: None Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        if nums == None or n <= 1: return
        i = n - 2
        j = n - 1
        while i >= 0 and nums[i] >= nums[i + 1]: i -= 1
        if i >= 0:
            while j >= 0 and nums[j] <= nums[i]: j -= 1
            nums[i] , nums[j] = nums[j], nums[i]
        start = i + 1
        end = n - 1
        while start < end:
            nums[start] , nums[end] = nums[end], nums[start]
            start, end = start + 1, end - 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值