Python | Leetcode Python题解之第31题下一个排列

题目:

题解:

class Solution:
    def nextPermutation(self, nums: List[int]) -> None:
        i = len(nums) - 2
        while i >= 0 and nums[i] >= nums[i + 1]:
            i -= 1
        if i >= 0:
            j = len(nums) - 1
            while j >= 0 and nums[i] >= nums[j]:
                j -= 1
            nums[i], nums[j] = nums[j], nums[i]
        
        left, right = i + 1, len(nums) - 1
        while left < right:
            nums[left], nums[right] = nums[right], nums[left]
            left += 1
            right -= 1

相关推荐

  1. 31. 一个排列

    2024-04-21 11:36:02       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-21 11:36:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-21 11:36:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-21 11:36:02       82 阅读
  4. Python语言-面向对象

    2024-04-21 11:36:02       91 阅读

热门阅读

  1. uniapp封装websocket以及心跳检测、重连

    2024-04-21 11:36:02       35 阅读
  2. 十、使用repo管理yocto各个layer

    2024-04-21 11:36:02       40 阅读
  3. asp.net core mvc 路由

    2024-04-21 11:36:02       36 阅读
  4. web server apache tomcat11-04-manager 如何管理?

    2024-04-21 11:36:02       36 阅读
  5. npm 常用命令详解

    2024-04-21 11:36:02       41 阅读
  6. C# 匿名方法与扩展方法详解

    2024-04-21 11:36:02       38 阅读
  7. axios的跨越封装

    2024-04-21 11:36:02       33 阅读
  8. 贝叶斯逻辑回归案例和使用场景

    2024-04-21 11:36:02       39 阅读
  9. 事务的隔离级别

    2024-04-21 11:36:02       33 阅读