题目描述:

给你一个正整数的数组 A(其中的元素不一定完全不同),请你返回可在 一次交换(交换两数字 A[i]和 A[j] 的位置)后得到的、按字典序排列小于 A 的最大可能排列。

如果无法这么操作,就请返回原数组。

示例 1:

输入:[3,2,1]
输出:[3,1,2]
解释:
交换 2 和 1

示例 2:

输入:[1,1,5]
输出:[1,1,5]
解释:
这已经是最小排列

示例 3:

输入:[1,9,4,6,7]
输出:[1,7,4,6,9]
解释:
交换 9 和 7 思路:
对数组A(A.size()=n),从后往前遍历,找到第一个A[i]<A[i-1]的i(i>0,找不到说明没办法调整,返回A),然后在下标i到n-1的数中找到小于A[i-1]的最大值max,找到下标i到n-1数中第一个等于max的j,交换A[j]和A[i-1]. C++:
class Solution {
public:
vector<int> prevPermOpt1(vector<int>& A) {
if (A.size() == ) return A;
for (int i = A.size() - ; i > ; i--){
if (A[i] < A[i - ]){
int max = A[i];
for (int j = i; j < A.size(); j++){
if (A[j] < A[i - ] && max < A[j]) max = A[j];
}
for (int j = i; j < A.size(); j++){
if (A[j] == max){
A[j] = A[i - ];
A[i - ] = max;
return A;
}
}
}
}
return A;
}
};

python:

class Solution:
def prevPermOpt1(self, A: List[int]) -> List[int]:
if len(A)==1: return A
for i in range(len(A)-1,0,-1):
if A[i]<A[i-1]:
max=A[i]
for j in range(i,len(A)):
if A[j]<A[i-1] and max<A[j]:
max=A[j]
for j in range(i,len(A)):
if A[j]==max:
A[j]=A[i-1]
A[i-1]=max
return A
return A

C++运行时间152 ms,暂时超过90.57%的提交,目前最快的解法112ms,code:

class Solution {
public:
vector<int> prevPermOpt1(vector<int>& A) {
int length = A.size();
if(length <= ){
return A;
}
int flag = false;
int i = ;
int index = -;
while(i < length){
if(A[i] < A[i - ]){
index = i - ;
}
i++;
}
if(index != -){
int swapIndex = index + ;
int maxTemp = A[index + ];
for(int j = index + ; j < length; j++){
if(A[j] < A[index] && A[j] > maxTemp){
maxTemp = A[j];
swapIndex = j;
}
}
A[swapIndex] = A[index];
A[index] = maxTemp;
}
return A;
}
};
 

leetcode-1053. 交换一次的先前排列的更多相关文章

  1. SQL语句中IF的简单使用 - 关联leetcode 627.交换工资

    MySQL的IF既可以作为表达式用,也可在存储过程中作为流程控制语句使用,如下是做为表达式使用: IF表达式 IF(expr1,expr2,expr3) 如果 expr1 是TRUE (expr1 & ...

  2. LeetCode 31. Next Permutation (下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  3. “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer ...

  4. LeetCode(31): 下一个排列

    Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...

  5. #leetcode刷题之路31-下一个排列

    实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列.如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列).必须原地修改,只允许使用额外常数空间. 以下 ...

  6. leetcode 31. Next Permutation (下一个排列,模拟,二分查找)

    题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...

  7. 【LeetCode】1409. 查询带键的排列 Queries on a Permutation With Key

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcode ...

  8. LeetCode 笔记21 生成第k个排列

    题目是这样的: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...

  9. 【LeetCode每天一题】Permutation Sequence(排列序列)

    The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...

随机推荐

  1. dubbo系列学习好文章

    1. dubbo入门学习(一)-----分布式基础理论.架构发展以及rpc.dubbo核心概念 https://www.cnblogs.com/alimayun/p/10982650.html 2. ...

  2. linux shell unzip multiple zip files

    find . -name "*.result.zip" | xargs -n 1 unzip - -P password -d ../ext_logs

  3. 卷积实现 python

    import sys h, w = input().strip().split() h = int(h) w = int(w) img = [] for i in range(h): line = s ...

  4. linux Cron 定时任务(centos 7.2 测试可用)

    1.Cron(学习笔记) 计划任务,是任务在约定的时间执行已经计划好的工作. 格式如下 Seconds Minutes Hours DayofMonth Month DayofWeek Year    ...

  5. Redis哨兵机制(sentinel)

    1.简介: 1.是什么: Redis-Sentinel是Redis官方推荐的高可用(HA)方案,当用Reids 做master-slave高可用方案时,假如master宕机了,redis本身(包括它的 ...

  6. Oracle使用存储过程返回查询游标

    如果报表逻辑非常复杂的话, 可以把报表逻辑放到存储过程里,加工一个全局临时表.前端查询的时候只查询临时表即可.只是第一次查询需要忍受加工的时间. --创建存储过程,返回SYS_REFCURSOR CR ...

  7. ORACLE PL、SQL编程

    PL(Procedural Language)过程化的编程语言,是在SQL的基础上增加的部分,如:变量的使用.流程控制等, 重点学习Oracle和MySQL创建存储过程及流程控制的异同. 一.存储过程 ...

  8. cmake 加入调试信息

    1 首先在CMakeLists.txt中加入 SET(CMAKE_BUILD_TYPE "Debug")1在下面加入: SET(CMAKE_CXX_FLAGS_DEBUG &quo ...

  9. react todelist

    1.点击按钮提交,新增对象 buttonChange() { this.setState({ //展开运算符...this.state.list,生成一个全新的数组 // list:[...this. ...

  10. vue eslint修改为4个空格