力扣——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 ...
随机推荐
- ltp-ddt smp_basic
SMP_S_FUNC_DUAL_CORE source functions.sh; cmd="stress-ng --matrix 4 -t 10s --perf --matrix-size ...
- 排查Java高CPU占用原因
近期java应用,CPU使用率一直很高,经常达到100%,通过以下步骤完美解决,分享一下. 方法一: 转载:http://www.linuxhot.com/java-cpu-used-high.htm ...
- vue项目中使用echarts地图
第一步.npm install echarts 第二部.在main.js中引入 第三步.创建组件,并且用this.$echarts.init初始化echarts <template> &l ...
- dotnet 跨平台编译发布
dotnet publish 命令,bash脚本如下(Windows安装git即可建议sh关联) publish.sh #!/usr/bin/env bash # one line command: ...
- nofollow标签的作用 nofollow标签添加方法
这篇文章主要介绍了nofollow标签的作用 nofollow标签添加方法,需要的朋友可以参考下 nofollow标签的作用 nofollow标签添加方法 模拟搜狗蜘蛛 nofollow标签 ...
- linux0.11内核源码——用户级线程及内核级线程
参考资料:哈工大操作系统mooc 用户级线程 1.每个进程执行时会有一套自己的内存映射表,即我们所谓的资源,当执行多进程时切换要切换这套内存映射表,即所谓的资源切换 2.但是如果在这个进程中创建线程, ...
- 【Java】Java引用maven私服jar包及jar包提交私服问题
pom.xml中加入以下配置即可 1.引用私服jar包 <!-- 加载的是 第三方项目使用的jar包 --> <repositories> <repository> ...
- MIS(管理信息系统)
MIS 管理信息系统(Management Information System,简称MIS) 是一个以人为主导,利用计算机硬件.软件.网络通信设备以及其他办公设备,进行信息的收集.传输.加工.储存. ...
- 2019-05-16 Ubuntu使用
Ubuntu的基本操作 查看操作系统版本 https://www.hostingadvice.com/how-to/ubuntu-show-version/ clu@sha01vmdev08:~/so ...
- error C2065: “CString”: 未声明的标识符 ;fatal error C1189: #error : afxstr.h can only be used in MFC proje
转自VC错误:http://www.vcerror.com/?p=1388 问题描述: error C2065: "CString": 未声明的标识符 fatal error C1 ...