Next Permutation

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

 
 
先从后往前寻找,找到首次下降的位置i      (找到进位的位置)
从位置i开始,往后寻找,找到第一个小于等于num[i]的位置j,  交换i和j-1(找到进位后,最高位的值)
将i+1后面的元素逆序排列 (取最小)
 
用一个例子来说明,比如排列是(2,3,6,5,4,1),求下一个排列的基本步骤是这样:
1) 先从后往前找到第一个不是依次增长的数,记录下位置p。比如例子中的3,对应的位置是1;
2) 接下来分两种情况:
    (1) 如果上面的数字都是依次增长的,那么说明这是最后一个排列,下一个就是第一个,其实把所有数字反转过来即可(比如(6,5,4,3,2,1)下一个是(1,2,3,4,5,6));
    (2) 否则,如果p存在,从p开始往后找,找到下一个数就比p对应的数小的数字,然后两个调换位置,比如例子中的4。调换位置后得到(2,4,6,5,3,1)。最后把p之后的所有数字倒序,比如例子中得到(2,4,1,3,5,6), 这个即是要求的下一个排列。

以上方法中,最坏情况需要扫描数组三次,所以时间复杂度是O(3*n)=O(n),空间复杂度是O(1)。代码如下:

 
 class Solution {
public:
void nextPermutation(vector<int> &num) { int n=num.size();
int i,j;
for(i=n-;i>=;i--)
{
if(num[i]<num[i+])
{
break;
}
}
if(i>=)
{
for(j=i+;j<n;j++)
{
if(num[j]<=num[i])
{
break;
}
}
j--;
swap(num[i],num[j]);
} reverse(num.begin()+i+,num.end());
} void swap(int &a,int &b)
{
int tmp=a;
a=b;
b=tmp;
}
};

【leetcode】Next Permutation的更多相关文章

  1. 【LeetCode】567. Permutation in String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...

  2. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

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

  3. 【leetcode】Next Permutation(middle)

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

  4. 【LeetCode】60. Permutation Sequence

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

  5. 【LeetCode】060. Permutation Sequence

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

  6. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  9. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

随机推荐

  1. HTTP1.0与HTTP1.1的区别

    HTTP/1.1与HTTP/1.0的区别 下面主要从几个不同的方面介绍HTTP/1.0与HTTP/1.1之间的差别,当然,更多的内容是放在解释这种差异背后的机制上. 1 可扩展性 可扩展性的一个重要原 ...

  2. jQuery,title、仿title功能整理

    如图:仿 title="查看" note="查看",note 可换成其他 样式: /*重写,标签title层*/#titleRewrite {position: ...

  3. Java并发编程核心方法与框架-CyclicBarrier的使用

    CyclicBarrier类似于CountDownLatch也是个计数器,不同的是CyclicBarrier数的是调用了CyclicBarrier.await()进入等待的线程数,当线程数达到了Cyc ...

  4. php之thinkphp部署Linux

    今天在学习thinkphp时遇到很多的问题,为了能够更好的学习今天抽出下午时间,对lamp环境下的开发进行了一些尝试,毕竟以前做过很多与Linux相关的工作,再加上php本身最优的搭配就是lamp环境 ...

  5. 电脑开机黑屏,显示Reboot and Select proper boot device!

    “reboot and select proper boot device or insert boot media in selected boot device and press a key” ...

  6. SQL Server数据库邮件配置

    一.数据库邮件介绍 数据库邮件是从SQL Server数据库引擎中发送电子邮件的企业解决方案,通过使用数据库邮件,数据库应用程序可以向用户发送电子邮件.邮件中可以包含查询结果,还可以包含来自网络中任何 ...

  7. [AngularJS] 入门

    什么是AngularJS AngularJS是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方面的不足, 其通过使用指令(directives)结构来扩展HTML词汇 ...

  8. 2013区域赛长沙赛区现场赛 K - Pocket Cube

    K - Pocket Cube Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  9. java项目报junit 相关错误

    maven配置,java工程运行时需要把test测试相关移除

  10. linux 分区问题

    一.最简单的分区 仅分出根目录(/)和最简单的内存置换空间(swap)即可 以虚拟机的分区(20G)为例: 一般给/分15G来安装linux系统,512M给swap,另外的4G可以留下备用 二.复杂一 ...