LintCode 190: Next Permutation

题目描述

给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列。

如果没有下一个排列,则输出字典序最小的序列。

样例

左边是原始排列,右边是对应的下一个排列。

1,2,3 → 1,3,2

3,2,1 → 1,2,3

1,1,5 → 1,5,1

Fri Feb 24 2017

思路

先看一个例子:

8, 5, 3, 7, 6, 5, 4, 1

下一个排列应该是:

8, 5, 4, 1, 3, 5, 6, 7

看起来应该是要从后往前找,找到第一个降序的数字,因为如果一直都是升序的话,说明这一部分以及是全排列的最大情况了。第一个比它后一个数小的数,就是需要替换为比他大一点的那个数。在上面的例子中就是3.

找到3以后,需要把它替换成一个比它大一点的数,于是再从后往前找,找到第一个比它大的数,在上面的例子中,就是4左边是原始排列,右边是对应的下一个排列。

34交换,由于后半部分刚才已经是最大的全排列了,所以需要翻转一下,变成最小的全排列。

整个过程大概如下:

8, 5, 3, 7, 6, 5, 4, 1

8, 5, 3, 7, 6, 5, 4, 1

8, 5, 3, 7, 6, 5, 4, 1

8, 5, 4, 7, 6, 5, 3, 1

8, 5, 4, 7, 6, 5, 3, 1

8, 5, 4, 1, 3, 5, 6, 7

考虑一些特殊情况,若数组长度小于等于1,则下一个排列就是它本身,所以直接返回。

若数组本身就是最大的全排列,则在第一次从前往后寻找第一个比它后一个数小的数时,找到最前面也找不到,于是就不需要找第二个数了,直接将整个数组翻转就好了。

代码

// 下一个排列
void nextPermutation(vector<int> &nums)
{
if (nums.size() <= 1) return;
vector<int>::iterator l = nums.end() - 2;
for (; l >= nums.begin(); --l)
if (*l < *(l + 1)) break;
if (l >= nums.begin())
{
vector<int>::iterator r = nums.end() - 1;
for (; r != l; --r)
if (*r > *l) break;
swap(*l, *r);
}
reverse(l + 1, nums.end());
}

LintCode 190: Next Permutation的更多相关文章

  1. LintCode 388: Kth Permutation

    LintCode 388: Kth Permutation 题目描述 给定 n 和 k,求123..n组成的排列中的第 k 个排列. 样例 对于 n = 3, 所有的排列如下: 123 132 213 ...

  2. lintcode:next permutation下一个排列

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

  3. lintcode:previous permutation上一个排列

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

  4. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  5. lintcode Permutation Index

    题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...

  6. Lintcode: Permutation Index II

    Given a permutation which may contain repeated numbers, find its index in all the permutations of th ...

  7. LintCode "Previous Permutation"

    A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy ...

  8. lintcode :Permutation Index 排列序号

    题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有 ...

  9. 211. String Permutation【LintCode by java】

    Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...

随机推荐

  1. Hexo博客搭建全解

    [原创,转载请附网址:http://dongshuyan.top] 欢迎来到莫与的博客,第一篇记录了一下怎么写一篇博客,以方便之后写博客~ #从配置说起下载安装Git与Node.js略过 1.安装he ...

  2. css3 关于文字,字体属性(转载)

    1.text-overflow属性(实现省略号效果) text-overflow用来设置是否使用一个省略标记(…)标示对象内文本的溢出. [语法] ❤text-overflow只是用来说明文字溢出时用 ...

  3. 遍历frame中的表单:

    遍历frame中的表单: public void table1() { // 查找frame List<WebElement> iframes = driver.findElements( ...

  4. 【C】多线程编程笔记

    1. pthread_create(pthread类型指针变量 ,NULL ,函数 ,函数参数[多个参数用结构体传]) 2. pthread_join(pthread类型指针变量, 返回一般为null ...

  5. this.$http & vue

    this.$http & vue https://github.com/pagekit/vue-resource Alias axios to Vue.prototype.$http http ...

  6. Debugger DataSet 调试时查看DataSet

    delphi  跟踪调试的时候查看DataSet数据记录 Ctrl+F7调试 增强工具DataSethttp://edn.embarcadero.com/article/40268 http://do ...

  7. [LeetCode] Climbing Sairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  8. bzoj4568-幸运数字

    题目 给出一棵树,每个节点上有权值\(a_i\),多次询问一条路径上选择一些点权值异或和最大值.\(n\le 2\times 10^4,q\le 2\times 10^5,0\le a_i\le 2\ ...

  9. 【uoj#51】[UR #4]元旦三侠的游戏 博弈论+dp

    题目描述 给出 $n$ 和 $m$ ,$m$ 次询问.每次询问给出 $a$ 和 $b$ ,两人轮流选择:将 $a$ 加一或者将 $b$ 加一,但必须保证 $a^b\le n$ ,无法操作者输,问先手是 ...

  10. Unbuntu+nginx+mysql+php

    1/准备 sudo su --切换到root 2/nginx安装 apt-get update apt-get install nginx 3/mysql 安装 apt-get install mys ...