dfs 之 下一个排列
52. 下一个排列
给定一个整数数组来表示排列,找出其之后的一个排列。
Example
例1:
输入:[1]
输出:[1]
例2:
输入:[1,3,2,3]
输出:[1,3,3,2]
例3:
输入:[4,3,2,1]
输出:[1,2,3,4]
Notice
排列中可能包含重复的整数
遇到这种题目,只能自己找找规律:
1 5 2 3 4 / /
1 5 2 4 3 (2 1) / \ / \
1 2 3 4 5 / down swap 2 only
5 4 3 2 1 \ up ==> 极端情形(独一) (1)场景
5 2 3 1 0 \ / \ up down ==> swap(min2(down), find greater than min2), then sort left (2)场景
基本上场景就是看你数据考虑是否全面。
通过观察总结起来的做法就是:
class Solution:
"""
@param nums: A list of integers
@return: A list of integers
"""
def nextPermutation(self, nums):
# write your code here
n = len(nums)
i = n-1
while i > 0 and nums[i] <= nums[i-1]:
i -= 1 if i == 0:
return nums[::-1] assert nums[i] > nums[i-1] greater_index = i
for j in range(i+1, n):
if nums[j] > nums[i-1]:
greater_index = j
else:
break assert nums[greater_index] > nums[i-1] nums[greater_index], nums[i-1] = nums[i-1], nums[greater_index] return nums[0:i] + sorted(nums[i:])
dfs 之 下一个排列的更多相关文章
- 排列算法汇总(下一个排列,全排列,第K个排列)
一.下一个排列 首先,STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. next_permutation(nums.begin() ...
- [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] ...
- C++构造 下一个排列 的函数
今天围观刘汝佳神犇的白书发现了一个好用的函数: next_permutation(); 可以用于可重, 或者不可重集, 寻找下一个排列. 时间复杂度尚不明. //适用于不可重和可重集的排列. # in ...
- LinkCode 下一个排列、上一个排列
http://www.lintcode.com/zh-cn/problem/next-permutation-ii/# 原题 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列 ...
- 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 ...
- LeetCode(31): 下一个排列
Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...
- 【LeetCode每天一题】Next Permutation(下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- git 学习网站
GitBook :https://git-scm.com/book/zh/v2 Git 教程 廖雪峰 :https://www.liaoxuefeng.com/wiki/89604348802960 ...
- 简单认识RTLO(Right-to-Left Override)
目录 两行代码实现字符逆序输出 做个假文件 参考 今天在群里看到的用法,RLO是一个微软的中东Unicode字符8238,或者0x202E,可以使后面的字符都变为RTL(阿拉伯语从右往左书写,对我们来 ...
- 基于32位Windows2003的数据库服务器优化,启用AWE,优化SQL Server
最近几天,笔者所在的单位中的一台WEB服务器由于负载过大出现了问题,当同时在线的用户达到一定规模(2000-3000)时,频繁出现页面响应迟缓.超时等问题.服务器采用的操作系统是Windows Ser ...
- es 内存占用优化
对6.3: 修改Elasticsearch中JVM配置文件jvm.options: Dlog4j2.enable.threadlocals=false 注: 本文主要针对ES 2.x. “该给ES分配 ...
- shell三剑客之grep
背景 对于很多的测试人员来说,grep命令都很熟悉,用的最多的比如去查找指定的进程:ps -ef | grep *** ,其中***为进程名或进程号,这里我们只用到的grep的最基础功能-从标准输出中 ...
- 040 RabbitMq及数据同步02
1.Spring AMQP (1)简介 Spring有很多不同的项目,其中就有对AMQP的支持: Spring AMQP的页面:http://spring.io/projects/spring-amq ...
- excel 使用
列于列拼接
- CRLF will be replaced by LF in XXX when git commit
转载自Git-warning: CRLF will be replaced by LF in XXX 今天,普通平凡的一天,平凡的使用 git add .,然后又出现一个之前没遇到的错误提示 . 真开 ...
- HTTP协议随笔
代理 代理就是处在客户端和服务端之间的服务器.客户端例如浏览器发送GET请求时,代理服务器接收该请求,并转发该请求至服务所在的服务器.服务器回复的数据和资源在第一时间经过代理服务器,才能回传到浏览器, ...
- Java+Python+前端 学习资料大全 百度云盘
Java架构师3大阶段 链接:https://pan.baidu.com/s/1DlXh33y5t4cZUmZH0cLvCw 提取码:5s76 2019前端架构阶段 链接:https://pan.ba ...