题目如下:

Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[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. 1 <= A.length <= 10000
  2. 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 bisectdic = {}val_list = []for i in range(len(A)-1,-1,-1):inx = bisect.bisect_left(val_list,A[i])inx -= 1if inx >= 0 and inx < len(val_list):A[i], A[dic[val_list[inx]]] = A[dic[val_list[inx]]], A[i]breakif A[i] not in dic:bisect.insort_left(val_list,A[i])dic[A[i]] = ireturn A

转载于:https://www.cnblogs.com/seyjs/p/10929238.html

【leetcode】1053. Previous Permutation With One Swap相关推荐

  1. 【LeetCode】031. Next Permutation

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  2. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  3. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  4. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  5. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  6. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

  7. 【Leetcode】103. 二叉树的锯齿形层次遍历

    题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...

  8. 【Leetcode】79.单词搜索

    题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格 ...

  9. 【leetcode】 算法题1 两数之和

    [leetcode] 算法题1 两数之和 问题   给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums ...

最新文章

  1. SpringMvc+ajax实现文件跨域上传
  2. JAVA构造方法,继承关系和SUPER关键字
  3. Linux mount/unmount命令
  4. 小蚂蚁学习C语言(35)——两行三列二维数组变三行两列二维数组
  5. xtrabackup 9.0备份出错的解决方法
  6. 计算机网络项目——最小网元设计(阶段四)
  7. Linux 文件与目录管理 | 菜鸟笔记收录
  8. NSURLConnection-网络访问(同步异步)
  9. CubeMX创建stm32项目及遇到的问题
  10. 如何安装VS2011Beta版
  11. 《因果推理原理:基础与学习算法》第一章 因果模型和统计模型
  12. win10 jungo windriver
  13. 申请德国农工大学计算机案例,【2014】德州农工大学:腼腆男的春天
  14. Echarts模板(一):南丁格尔玫瑰图
  15. Ubuntu 18.04上跑通LSD SLAM
  16. 风湿与类风湿的关系以及风湿病涉及基因突变与基因甲基化的文献收集(仍在更新。。。)
  17. TI达芬奇系列TMS320DM8168浮点DSP C674x + ARM Cortex-A8高性能视频处理器,用于视频基础设施
  18. html标记语言表格,表格《 HTML:标记语言 》
  19. TeamViewer由商业用途改为个人用途
  20. 你可以穿裙子,但是请放长你的裙摆

热门文章

  1. php order by 3-- 没反应,Thinkphp 无法使用-)order() 排序的两种解决办法!
  2. java jnlp 签名_JAVA JNLP组件数字签名制作步骤
  3. sqlserver 如何把一列分为一行显示_EXCEL神乎其技 多列追加为一列
  4. linux系统 安装mysql,在linux系统中安装MySQL
  5. 计算机省二c语言编程改错题,2013年计算机二级C语言上机试题六十九
  6. java 开发者异常处理_Java异常处理和设计
  7. java流程控制if_java程序流程控制(分支结构之 if-else)
  8. 三次样条插值matlab,Matlab关于三次样条插值
  9. vscode 智能提示失效
  10. 6.java中什么是类_类、对象(java基础知识六)