leetcode——31.下一个排列

题目描述:

实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。

如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。

必须原地修改,只允许使用额外常数空间。

以下是一些例子,输入位于左侧列,其相应输出位于右侧列。
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

1,3,2——2,1,3

我感觉我的做法并没有错啊,自己输出就是正确的,在上面运行就是错误的,可能是我不懂没有return是什么意思。。。。

class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: None Do not return anything, modify nums in-place instead.
        """
        if len(nums)<2:
            return nums
        if nums==sorted(nums,reverse=True):
            nums=sorted(nums)
            return nums
        i=len(nums)-1
        while i>0:
            if int(nums[i])> int(nums[i-1]):
                nums[i],nums[i-1]=nums[i-1],nums[i]
                return nums
            else:
                if len(nums[:-2])==1:
                    m=nums[0]#默认列表元素大于0
                    if nums[-1]>m:
                        nums[0],nums[1],nums[-1]=nums[-1],nums[0],nums[1]
                        return nums
                    else:
                        s=nums[0]
                        nums.pop(0)
                        nums.append(s)
                        return nums
                else:
                    m=min(nums[:-2])
                    if nums[i]>m:#如果有重复最小值
                        d=nums[::-1].index(m)
                        p=len(nums)-d-1
                        s=nums[i]
                        nums.pop(i)
                        nums.insert(p,s)
                        return nums
                    else:
                        i-=1

这是我的

这是别人的:

class Solution:
    def nextPermutation(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        firstIndex = -1
        n = len(nums)
        def reverse(nums, i, j):
            while i < j:
                nums[i],nums[j] = nums[j], nums[i]
                i += 1
                j -= 1
        for i in range(n-2, -1, -1):
            if nums[i] < nums[i+1]:
                firstIndex = i
                break
        #print(firstIndex)
        if firstIndex == -1:
            reverse(nums, 0, n-1)
            return 
        secondIndex = -1
        for i in range(n-1, firstIndex, -1):
            if nums[i] > nums[firstIndex]:
                secondIndex = i
                break
        nums[firstIndex],nums[secondIndex] = nums[secondIndex], nums[firstIndex]
        reverse(nums, firstIndex+1, n-1)

翻译过来:

先找出最大的索引 k 满足 nums[k] < nums[k+1],如果不存在,就翻转整个数组;
再找出另一个最大索引 l 满足 nums[l] > nums[k];
交换 nums[l] 和 nums[k];
最后翻转 nums[k+1:]。
举个例子:

比如 nums = [1,2,7,4,3,1],下一个排列是什么?

我们找到第一个最大索引是 nums[1] = 2

再找到第二个最大索引是 nums[5] = 3

交换,nums = [1,3,7,4,2,1];

翻转,nums = [1,3,1,2,4,7]

完毕!

所以,

时间复杂度:O(n)O(n)

空间复杂度:O(1)O(1)

作者:powcai
链接:https://leetcode-cn.com/problems/next-permutation/solution/xia-yi-ge-pai-lie-by-powcai/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

别人的答案在上面也运行不出来,不知道什么鬼。。。

                                    ——2019.10.13

猜你喜欢

转载自www.cnblogs.com/taoyuxin/p/11668144.html