题目链接

31. Next Permutation

题意

给定一段排列,输出其升序相邻的下一段排列。比如[1,3,2]的下一段排列为[2,1,3]。

注意排列呈环形,即[3,2,1]的下一段排列为[1,2,3]

思路

这个题蛮巧妙的,关键在于发现规律。假如给定的排列为[4,6,7,5],那么其下一段排列应该为[4,7,5,6]

我们可以看到除了首位的4保持不动外,后三位均发生了改变。我们可以把[4,6,7,5]看成是[4,6,5,7] -> [4,7,5,6]

这里的6称为旋转点,我们先把旋转点右边的升序([6,7,5] -> [6,5,7]),然后从升序的子数组里找到到第一个大于旋转点的值,然后交换两者的值

为什么要这么处理呢?我的一个理解是要保证相邻排列是升序的,不如先把旋转点右边的值先排序,至少保证了局部有序,

然后从局部有序的右边数组里面采用二分查找,找到第一个大于旋转点的值,交换即可满足题意。

那怎么定义旋转点呢?

可以从最右边开始往左边找,第一个小于右边相邻的数即为旋转点,如[4,6,7,5]从右边往左边找,第一个小于右边相邻的数为6(6<7, 7>5)

class Solution {
public:
//这个题,怎么说呢
//在于观察吧
//2 4 7 6-> 2 4 6 7->2 6 4 7
//模拟
void nextPermutation(vector<int>& nums) {
int n=nums.size();
if(n<=1) return ; int j=n-2; for(;j>=0;j--){//找到旋转点 if(nums[j]<nums[j+1]) break;
} //把旋转点右边的数组升序
reverse(nums.begin()+j+1,nums.end()); if(j==-1) return ; //找到和旋转点可以替换的点
auto it=upper_bound(nums.begin()+j+1,nums.end(),nums[j]); swap(*it,nums[j]); }
};

leetcode 31. Next Permutation (下一个排列,模拟,二分查找)的更多相关文章

  1. [LeetCode] 31. Next Permutation 下一个排列

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

  2. [leetcode]31. Next Permutation下一个排列

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

  3. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

  4. [LeetCode] Next Permutation 下一个排列

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

  5. 【LeetCode每天一题】Next Permutation(下一个排列)

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

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

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

  7. Next Permutation 下一个排列

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

  8. 031 Next Permutation 下一个排列

    实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...

  9. lintcode:next permutation下一个排列

    题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...

  10. 31. Next Permutation (下一个全排列)

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

随机推荐

  1. hadoop_MapReduce_idea上打jar包,在虚拟机上运行

    打包前的介绍和准备工作 指定主类可以在运行jar包的时候不用输入要运行哪一个类,直接就可以运行了 指定主类 编辑jar 的信息 修改jar包的名称 build Complete!!! MapReduc ...

  2. Fabric 配置 order节点问题

    问题描述: Error: failed to create deliver client: orderer client failed to connect to orderer.example.co ...

  3. ssl证书---验证等级分类

    DV SSL证书(domain Validation SSL): 指只验证网站域名所有权的简易型SSL证书,此类证书仅能起到网站机密信息加密的作用,无法向用户证明网站的真实身份.所以,不推荐在电子商务 ...

  4. 用DirectX 11绘制一个Cube

    之前一篇文章讲了如何初始化DirectX 11,现在在此基础上绘制一个Cube,总体可概括为以下几个步骤: 定义Cube顶点数据结构 创建Vertex Buffer和Index Buffer 编写应用 ...

  5. pandas的学习8-pandas-plot出图

    import pandas as pd import numpy as np import matplotlib.pyplot as plt ''' 这次我们讲如何将数据可视化. 首先import我们 ...

  6. PHP比较数组、对象是否为空

    PHP简单对比对象.数组是不是为空: 1 <?php 2 /*简单的比较对象和数组是不是为空*/ 3 4 #定义空类EmptyClass 5 class EmptyClass{} 6 7 $em ...

  7. PHP函数number_format()

    PHP的number_format() 函数通过千位分组来格式化数字. 语法: number_format(number,decimals,decimalpoint,separator) 注释:该函数 ...

  8. Vitis AI--个人调试篇

    一.下载VITIS-AI的仓库 单独git clone很慢,因此先将其导入到gitee平台,再执行clone 1. Import VITIS-AI github repo into gitee rep ...

  9. eclipse中安装Springboot的插件

    1help在Eclipse Marketplace中搜索spring-tool-suite,点击install,然后接受协议,等待重启eclipse即可

  10. Amazing 2020

    Amazing 2020 Intro 2020 转眼即逝,2020 是比较艰辛的一年,因为疫情原因,很多人的工作以及生活都多多少少受到了一些影响. 引用网上盛传的一句话--2020 实"鼠& ...