problem:

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

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

意思是给了左边的一个排列,给出它的下一个排列。下一个排列是指按词典序的下一个排列。降序的排列已经是按词典序的最大的排列了,所以它的下一个就按升序排列。

求下一个排列可以通过以下三步实现:

1.从后往前,找到第一个 A[i-1] < A[i]的。
2.从 A[i]到A[n-1]中找到一个比A[i-1]大的最小值(也就是说在A[i]到A[n-1]的值中找到比A[i-1]大的集合中的最小的一个值)
3.交换这两个值(A[i]和找到的值),并且把A[i]到A[n-1]进行排序,从小到大。
这样就是词典序的下一个排列了。
class Solution {
public:
void nextPermutation(vector<int> &num) {
if (num.size() == )
return;
int ind = -;
for (int i = num.size() - ; i > ; i--) // 是大于零或者大于等于1
{
if (num[i - ] < num[i])
{ind = i -;break;}
}
if (ind == -)
{sort(num.begin(),num.end());return;}
int min = INT_MAX, sw = -;
for (int j = num.size() -; j > ind; j--)
{
if(num[j] > num[ind] && num[j] < min)
{ min = num[j]; sw = j;}
}
int tmp = num[ind];
num[ind] = num[sw];
num[sw] = tmp;
vector<int>::iterator it = num.begin();
for(int i = ; i < ind + ; i++)
{
it++;
}
sort(it, num.end());
return;
}
};

这题考察什么是词典序的下一个排列。

leetcode第30题--Next Permutation的更多相关文章

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

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

  2. leetcode第30题:括号生成

    这是目前遇到最难的题,刚开始的思路是:匹配words中元素是否在s中,若在找所在元素的后words长度位的字符串,判断words其他元素是否都在s中. 看似这个思路可行,实际上存在的问题: 1.wor ...

  3. 乘风破浪:LeetCode真题_031_Next Permutation

    乘风破浪:LeetCode真题_031_Next Permutation 一.前言 这是一道经典的题目,我们实在想不出最好的方法,只能按照已有的方法来解决,同时我们也应该思考一下为什么要这样做?是怎么 ...

  4. leetcode top-100-liked-questions刷题总结

    一.起因 宅在家中,不知该做点什么.没有很好的想法,自己一直想提升技能,语言基础自不必言,数据结构还算熟悉,算法能力一般.于是乎,就去刷一通题. 刷题平台有很多,我选择了在leetcode进行刷题.回 ...

  5. 【python】Leetcode每日一题-扰乱字符串

    [python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...

  6. 【python】Leetcode每日一题-最大数

    [python]Leetcode每日一题-最大数 [题目描述] 给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数. 注意:输出结果可能非常大,所以你需要返回一个 ...

  7. 【python】Leetcode每日一题-丑数

    [python]Leetcode每日一题-丑数 [题目描述] 给你一个整数 n ,请你判断 n 是否为 丑数 .如果是,返回 true :否则,返回 false . 丑数 就是只包含质因数 2.3 和 ...

  8. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  9. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

随机推荐

  1. 走向DBA[MSSQL篇] 详解游标

    原文:走向DBA[MSSQL篇] 详解游标 前篇回顾:上一篇虫子介绍了一些不常用的数据过滤方式,本篇详细介绍下游标. 概念 简单点说游标的作用就是存储一个结果集,并根据语法将这个结果集的数据逐条处理. ...

  2. boost准模板库scoped_ptr指针的使用以及auto_ptr智能指针的对照

    首先我们看看scoped_ptr的基本使用,包括了swap(),get(),reset()的使用,重要的提醒是作用域结束的时候会自己主动析构,无需手动的释放资源: #include<boost/ ...

  3. Android 异步消息处理机制 让你在深入了解 Looper、Handler、Message之间的关系

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/38377229 ,本文出自[张鸿洋的博客] 非常多人面试肯定都被问到过,请问And ...

  4. Windows Cygwin Redis 安装(转)

    在win平台下编译Redis一般有两种方式: 1. 基于MS VC进行编译,生成原生可执行文件 该方式需要创建MSVC项目文件以及对Redis源码进行适当调整. 这里提供一个可行版本,由微软开放团队进 ...

  5. MTK6572横屏的调试过程

    电视剧集:系统MTK缺省的系统源代码,Phone模式.底部有三个虚拟按键.需求为,设置成默认横屏,设定一个合理的虚拟按键方案. ------------------------------------ ...

  6. 《Java并发编程实战》第十三章 显示锁 读书笔记

    一.Lock与 ReentrantLock Lock 提供一种无条件的.可轮询的.定时的.可中断的锁获取操作,全部加锁和解锁的方法都是显式的. public interface Lock { void ...

  7. Git合并多个Commit

    当前有四个commit,现在要将四个commit合并为一个,可以使用git rebase -i HEAD~{这里是要合并的commit数量} 如 git rebase -i HEAD~4 ,即为合并最 ...

  8. 图片 Base64码 转换

    import sun.misc.BASE64Decoder; private String getBase64Picture(String imgBase64Str) { FileOutputStre ...

  9. ORACLE查看和更改的最大连接数

     第一步,在cmd命令行,进入sqlplus 第二步骤,根据提示输入username与password 1. 视图processes和sessions参数 SQL> show paramet ...

  10. C#关于HttpClient的应用(二):极光推送IM集成

    public class JPushClient:BaseHttpClient { private String appKey; private String masterSecret; public ...