力扣——Next Permutation(下一个排列) python实现
题目描述:
中文:
实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。
如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。
必须原地修改,只允许使用额外常数空间
英文:
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.
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) <= 1:
return
for i in range(len(nums) - 2, -1, -1):
if nums[i] < nums[i + 1]:
for k in range(len(nums) - 1, i, -1):
if nums[k] > nums[i]:
nums[i], nums[k] = nums[k], nums[i]
nums[i+1:] = sorted(nums[i+1:])
break
break else:
if i == 0:
nums.sort()
题目来源:力扣
力扣——Next Permutation(下一个排列) python实现的更多相关文章
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- lintcode:next permutation下一个排列
题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...
- Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【LeetCode每天一题】Next Permutation(下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 031 Next Permutation 下一个排列
实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...
- LeetCode 31. 下一个排列 | Python
31. 下一个排列 题目 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改, ...
- [leetcode]31. Next Permutation下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [Swift]LeetCode31. 下一个排列 | Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- HIbernate 查询拼接参数
public List<TrailTestModel> findByEid(List<String> trailids, String eid) { // TODO Auto- ...
- UVa1636 Headshot 【迁移自洛谷博客】
说明:小蒟蒻hkk现在正在做一些概率的题目,由于这方面和数学还有点关系,所以需要一些数学的思维,也需要表述出来,如夏军所述"把自己给讲懂",所以写了些blog,主要为帮助自己理解. ...
- js 中HTML的 onkeycode 和onkeydown属性事件
<!DOCTYPE html><html><head><script>function displayResult(){var x=document.g ...
- 【leetcode】564. Find the Closest Palindrome
题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的.要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1 ...
- createElement 函数
我们知道,vue函数的渲染其实是由render函数的回调函数createElement 来创建的虚拟dom,那么它到底是怎么创建组件的? 尚未理解透彻[捂脸],有待补充,参考如下: https://w ...
- 基于oracle 的PL/SQL编程 - 存储过程
接上篇,游标使用的语句,相当于一段匿名的函数,窗口关闭了就不存在了.如果想要窗口关闭了,还能继续执行那段代码,就需要存储过程了: PLSQL是指一个个PLSQL的业务处理过程存储起来进行复用,这些被存 ...
- sql查询语句得到返回值fetchone()
需求: 现在mysql中有一张表,表名是info,我想通过报案号4201820330114401021497在这张表里查询出它对应的id. sql = "select claim_id fr ...
- nb哒LCA
求欧拉序每log分一块每段找最小值共n/log块然后建st表,复杂度n/log*log = n每块记前后缀最小过至少一块很好求对于在一块的:由于欧拉序的标号前后只会相差1所以序列种类只有2^k种k&l ...
- 三线SWD模式Jlink
三线SWD模式Jlink 在公司实习,部门经理让我做一个USB-CAN的适配器. 在网上找资料,找到一个开源的USB-CAN的适配器的资料. 采用的是CP2102芯片实现USB转串口.STM32作 ...
- Hibernate中常用HQL
HQL是Hibernate自带的查询语言 HQL是一种面向对象的查询语言.SQL的操作对象是数据表和列等数据对象,而HQL的操作对象是类.实例.属性等. HQL的语法很像SQL的语法 以下举例均以学生 ...