【leetcode】1053. Previous Permutation With One Swap
题目如下:
Given an array
A
of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller thanA
, that can be made with one swap (A swap exchanges the positions of two numbersA[i]
andA[j]
). If it cannot be done, then return the same array.Example 1:
Input: [3,2,1]
Output: [3,1,2]
Explanation: Swapping 2 and 1.Example 2:
Input: [1,1,5]
Output: [1,1,5]
Explanation: This is already the smallest permutation.Example 3:
Input: [1,9,4,6,7]
Output: [1,7,4,6,9]
Explanation: Swapping 9 and 7.Example 4:
Input: [3,1,1,3]
Output: [1,3,1,3]
Explanation: Swapping 1 and 3.Note:
1 <= A.length <= 10000
1 <= A[i] <= 10000
解题思路:要找出字典序小于自己的最大值,方法如下:从后往前遍历A,对于任意一个A[i],在[i+1,A.length]区间内找出比自己小的最大值,如果能找到这样的值,则这两个元素交换,交换之后的A即为字典序小于自己的最大值。怎么找出[i+1,A.length]区间内找出比自己小的最大值?可以把区间内所有的值存入有序的数组中,通过二分查找即可。
代码如下:
class Solution(object):
def prevPermOpt1(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
import bisect
dic = {}
val_list = []
for i in range(len(A)-1,-1,-1):
inx = bisect.bisect_left(val_list,A[i])
inx -= 1
if inx >= 0 and inx < len(val_list):
A[i], A[dic[val_list[inx]]] = A[dic[val_list[inx]]], A[i]
break
if A[i] not in dic:
bisect.insort_left(val_list,A[i])
dic[A[i]] = i
return A
【leetcode】1053. Previous Permutation With One Swap的更多相关文章
- 【LeetCode】31. Next Permutation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...
- 【LeetCode】31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【LeetCode】31. Next Permutation (2 solutions)
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...
- 【LeetCode】031. Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- 【leetcode】266. Palindrome Permutation
原题 Given a string, determine if a permutation of the string could form a palindrome. For example, &q ...
- 【LeetCode】266. Palindrome Permutation 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- leetcode_1053. Previous Permutation With One Swap
1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
随机推荐
- Python编程:从入门到实践—类
创建类 #!/usr/bin/env python# --*-- encoding:utf-8 --*-- class Dog(): """一次模拟小狗的简单尝试&quo ...
- astype()函数
1astype()函数可用于转化dateframe某一列的数据类型 如下将dateframe某列的str类型转为int,注意astype()没有replace=True的用法,想要在原数据上修改,要写 ...
- win10+jdk+mysql+tomcat+jpress环境搭建与部署
本机搭建jpress用于接口测试的学习 目录 1.环境与工具准备 2.mysql服务端安装 3.tomcat配置 4.jpress部署 1.环境与工具准备 a.服务器为本机为win10 64位 b.j ...
- webpack前端模块打包器
webpack前端模块打包器 学习网址: https://doc.webpack-china.org/concepts/ http://www.runoob.com/w3cnote/webpack-t ...
- python3爬虫之urllib初探
urllib主要包含request(请求模块).error(异常处理模块).parse(工具模块).robotparser(识别网站的robots.txt文件,是否允许爬取). request(请求模 ...
- 简述移动端与PC端的区别
1.移动端与PC端的区别 PC考虑的是浏览器的兼容性,而移动端开发考虑的更多的是手机兼容性,因为目前不管是android手机还是ios手机,一般浏览器使用的都是webkit内核,所以说做移动端开发,更 ...
- 加密算法:DES、AES等
指标:运算速度.安全性.资源消耗 对称加密算法(加解密密钥相同): 非对称算法(加密密钥和解密密钥不同): 散列算法比较: 对称与非对称算法比较: 算法选择(从性能和安全性综合) 对称加密: AES( ...
- MongoDB 基本操作(增改删)
1.插入数据 和关系型数据库一样,增加数据记录可以使用insert语句,这是很简单的. 当插入数据时,如果此集合不存在,则MongoDB系统会自动创建一个集合,即不需要刻意预先创建集合 每次插入数据时 ...
- Nhbernate
一.ORM1.对象关系映射(Object Relational Mapping,简称ORM)是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.2.ORM是通过使用描述对象和数据库之间映射的 ...
- 时间戳位数不够13位,通过es6 的padEed补零
if(val.toString().length == 10){ val = val.toString().padEnd(13,"0")*1 //不够十三位放后面补零,超过13位也 ...