LeetCode-31. Next Permutation

6 篇文章 0 订阅
4 篇文章 0 订阅

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

 

一开始想的是从所给序列的元素在字典序下的第一个排列开始往后生成排列,直到所给的序列出现,让nums = 其下一个排列,然后担心这样当数据所给情况不好时会超时,在网上搜了一下解法,觉得这个方法不错,引用一下

下面的方法来自:解法来源

 

字典序法

对于算法运行时得到的一个排列,如list=[2,4,3,1],如何得到这个排列的下一个排列呢?

  1. 由尾部向前寻找第一个满足before<after的元素,并记下其下标low,在本例中即2对应low=0
  2. 从low的后一个元素开始向后寻找最后一个满足list[high]>list[low]的元素,并记下其下标high,在本例中即3对应high=2
  3. list[low]list[high]对换,得到list=[3,4,2,1]
  4. 将下标low之后的所有元素翻转,得到list=[3,1,2,4]

这样就得到了[2,4,3,1]字典序下的后一个排列[3,1,2,4]

 


然后改方法出处给了一段python代码,我写完了我自己的才去看的,发现我对python的理解真的是辣鸡啊,人家写的又好又简洁:

def permutations(n):
    indices = list(range(n))
    print(indices)
    while True:
        low_index = n-1
        while low_index > 0 and indices[low_index-1] > indices[low_index]:
            low_index -= 1
        if low_index == 0:
            break
        low_index -= 1
        high_index = low_index+1
        while high_index < n and indices[high_index] > indices[low_index]:
            high_index += 1
        high_index -= 1
        indices[low_index], indices[high_index] = indices[
            high_index], indices[low_index]  #这里交换的操作我第一次看真的是神了
        indices[low_index+1:] = reversed(indices[low_index+1:])  #这句可以,很强,我写的时候还在想怎么翻转list中的一部分,不知道,所以我干脆自己写了个while循环...  
        print(indices)  

不过这段代码的作者想要实现的效果和题目不一样,他只是要打印出所有的排列情况而已,他对于3,2,1这种字典序排列就没有做处理了,因为对于他的目的而言,这是最后一个排列直接打印出来就好了,但是在本题,要对应的输出第一个排序的序列1,2,3

我考虑到了(...是在提交出错了之后,,)下面是我辣鸡的solution:

class Solution:
    def nextPermutation(self, nums: List[int]) -> None:
        low = 0
        high = 0
        numsLen = len(nums)
        i = numsLen - 1
        flag = 0
        while i>0:
            if nums[i-1]<nums[i]:
                flag = 1
                low = i -1
                break
            i-=1
        if flag == 0:
            nums.sort()
            return
        i = numsLen -1
        while i > low:
            if nums[i]>nums[low]:
                high = i
                break
            i-=1

        num = nums[low]
        nums[low] = nums[high]
        nums[high] = num

        i = low +1
        k = 0
        while i <= (low+1+numsLen-1)/2:
            temp = nums[i]
            nums[i] = nums[numsLen-1-k]
            nums[numsLen-1-k] = temp
            k+=1
            i+=1

真的要好好了解python list和下标的slice notation的相关操作啊,要不然代码写这么长(笑哭)...

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值