【leetcode】1053. Previous Permutation With One Swap
题目如下:
Given an array
Aof 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 <= 100001 <= 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 ...
随机推荐
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_06 Properties集合_3_Properties集合中的方法load
键值对文件,读取到集合中来使用 分隔符也可以用空格 读出来乱码
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_10_字节输入流一次读取一个字节的原理
原理解析 创建一个字节流,指向读取文件的第一个字节. read找jvm,jvm找os.os去读取硬盘.,读取后指正向后移动一位
- MySQL 修改密码,
ALTER USER 'root'@'localhost' IDENTIFIED BY 'admin';
- Package manager has died异常PackageInfo 引发 Crash
Android 获取 PackageInfo 引发 Crash 填坑 一般 Android 通过PackageInfo这个类来获取应用安装包信息,比如应用内包含的所有Activity名称.应用版本号之 ...
- 自动化测试--利用opencv进行图像识别与定位
SIFT检测方法 SIFT算法就是把图像的特征检测出来,通过这些特征可以在众多的图片中找到相应的图片 import cv2 #读取图片,以1.png为例 img=cv2.imread('1.png') ...
- 【Unity Shader】---准确认识SubShader语义块结构、渲染状态设定、Tags标签
一[SubShader] 每个UnityShader文件可以包含多个SubShader语义块,但至少要有一个.当Unity需要加载这个UnityShader时,Unity会扫描所有的SubShader ...
- html5 WebSocket的Js实例教程
详细解读一个简单+ ,附带完整的javascript websocket实例源码,以及实例代码效果演示页面,并对本实例的核心代码进行了深入解读. 从WebSocket通讯三个阶段(打开握手.数据传递. ...
- mybatis初步理解
mybatis概念 mybatis 是一款轻量级的orm的数据持久框架,封装jdbc 对开发提供了便利,但是性能会比jdbc低,从开发的角度来说,现在是比较流行的 掌握上比较容易,也支持缓存,级联 ...
- Maven系列学习(三)Maven生命周期和插件
Maven生命周期和插件 Maven另外的两个核心概念就是生命周期和插件,Maven的生命周期都是抽象的,其实实际行为都是由插件来完成的,生命周期和插件两者协同工作 1.生命周期 Maven的生命周期 ...
- Java学习day7面向对象编程1-对象和类
一.Java编程中对象和类的概念 1,什么是类? 答:类是客观存在的,抽象的,概念的东西. 2,什么是对象? 答:对象是具体的,实际的,代表一个事物.例如:车是一个类,汽车,自行车就是他的对象. 关于 ...