【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 ...
随机推荐
- 在静态页面中使用 Vue.js
在静态页面中使用 Vue.js 不使用Node.js, NPM, Webpack 等, 在静态页中使用Vue.js. 包括路由, 单文件组件. 1. 创建index.html index.html做为 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第5节 String类_1_字符串概述和特点
在api中查找 java.lang包里面的不用引用
- list、tuple、dict加*星号
*相当于解压的作用,当list中只有一个元素时,利用*作用相当于直接去[],去[]后的数据类型是字符串. _list = [1,2,3] _tuple = (1,2,3) _dict = {1:'a' ...
- UI自动化之特殊处理二(弹框\下拉框\选项\文件上传)
弹框\下拉框\选项\文件上传也是一些比较特殊的操作 目录 1.弹框 2.下拉框 3.选项 4.文件上传 1.弹框 弹框有三种形式,value为alert.confirm.prompt三种的弹框,第一个 ...
- Caffe:深入分析(怎么训练)
main() 首先入口函数caffe.cpp int main(int argc, char** argv) { ...... ) { #ifdef WITH_PYTHON_LAYER try { # ...
- Linux pip命令报错 -bash: pip: command not found
下载 $ wget https://bootstrap.pypa.io/get-pip.py 安装 $ python get-pip.py 查看版本 $ pip -V 查看安装路径: find / - ...
- 【EWM系列】SAP EWM WCU和Non-SAP系统接口
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP EWM WCU和Non-SA ...
- SSM003/构建Maven单模块项目(一)
一.环境准备 1.开发工具:IDEA 2.JDK版本:jdk1.8 3.Maven版本:apache-maven-3.2.5 4.数据库mysql. 二.基于Maven构建web项目 Step1:Fi ...
- vue中的computed 与 watch
计算属性 computed 指通过计算得来的属性,用于监听属性的变化 computed里面的函数调用的时候 不需要加() 方法里必须有一个返回值 return computed中的函数不会通过事件去触 ...
- OuterXml和InnerXml(2)
官方例子:https://msdn.microsoft.com/en-us/library/system.xml.xmlnode.outerxml.aspx using System; using S ...