31. Next Permutation

Total Accepted: 54346 Total
Submissions: 212155 Difficulty: Medium

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

STL来做,秒杀

class Solution {
public:
void nextPermutation(vector<int>& nums) {
next_permutation(nums.begin(), nums.end());
}
};

此题感觉没多大意思。不好高速想出规律:

可是比較明显的规律是:

1。升序为最小组合,降序为最大组合

2,某一个数的下一个数,就是比他大的最小组合数。比方123,下一个比他大的最小组合就是132,必须从低位处理

3,stl的处理就是从低位(数组末尾)開始找起,

a)找到首个相邻的升序序列。将前一个数据与比其首次大的数交换(然后逆置后面的数)比方:123543为124533(首个升序3,5。可是4是首次比3大,则交换),此处的操作意义就是使数通过首个升序变大。

b)显然此数不是大于原数的最小数,而且后面的数逆置后才是最小的,即124533为124335。so,done!

//思路首先(不调用库函数):
//举例:abc,acb,bac,bca,cab,cba
class Solution {
public:
void nextPermutation(vector<int>& nums) {
if(nums.empty() || nums.size()==1)
return;
vector<int>::iterator ite1=nums.end();
ite1--;
while(true)
{
vector<int>::iterator ite2=ite1;
ite1--;//ite1始终在ite2前面一个位置(左边算作最前面)
if(*ite1 < *ite2)//升序已经出现。接着又一次从后面找首个升序位置
{
vector<int>::iterator itej=nums.end();
while(!(*ite1 < *--itej));
iter_swap(ite1,itej);//找到首个升序序列将其交换
reverse(ite2,nums.end());
return;
} if(ite1==nums.begin())
{
reverse(nums.begin(),nums.end());
return;
}
}
}
};

注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50450861

原作者博客:http://blog.csdn.net/ebowtang

參考资源

【1】侯捷。《STL源代码剥析》

&lt;LeetCode OJ&gt; 31. Next Permutation的更多相关文章

  1. leetcode个人题解——#31 Next Permutation

    写这题时脑子比较混乱,重写了一遍wiki大佬的解法. 算法: According to Wikipedia, a man named Narayana Pandita presented the fo ...

  2. [Leetcode][Python]31: Next Permutation

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...

  3. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  4. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  5. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

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

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

  7. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  8. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  9. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

随机推荐

  1. Django 创建新项目后要完成的几个步骤

    首先,在过一遍创建新项目的步骤: -创建一个新项目 -建了数据库后要确定自己是用 mysql数据库  还是用 sqlite3数据库 -如果是mysql数据库,那一堆配置 -如果是sqlite3数据库, ...

  2. 缓存,队列(Redis,RabbitMQ)

    Redis Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...

  3. P1375 嵌套矩形

    题目Problem 嵌套矩形 Time Limit: 1000ms    Memory Limit: 131072KB 描述Descript. 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形 ...

  4. 解决Latex复制到公众号可能报“图片粘贴失败”的问题

    前几天出了个版本,还发了篇“Md2All,让公众号完美显示Latex数学公式”的文章,发完后,心里还是不太爽的,因为那个版本还是遗留了一个问题:当把Latex公式转换为本地图片,再复制到公众号时,有可 ...

  5. HEK_费用报表审核无审核权限,有些字段无法编辑的问题处理

    Q:HEK_费用报表审核无审核权限,有些字段无法编辑的问题处理 A:设置AP员工->给AP员工分配审批权限->绑定员工和ERP账号 1.将审核人设置为AP员工 2.分配给员工审批权限 3. ...

  6. SqlSever2005 一千万条以上记录分页数据库优化经验总结【索引优化 + 代码优化】

    对普通开发人员来说经常能接触到上千万条数据优化的机会也不是很多,这里还是要感谢公司提供了这样的一个环境,而且公司让我来做优化工作.当数据库中的记录不超过10万条时,很难分辨出开发人员的水平有多高,当数 ...

  7. electron 学习

    index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  8. 【ng-zorro-antd】加入in-memory-web-api插件后icon不显示

    在ng-zorro-antd框架下,根据angular的技术文档demo,加入in-memory-web-api后icon不显示 解决方案: 在app.module.ts中的imports:[]加入 ...

  9. C#—接口和抽象类的区别?

    一.接口 接口是指对协定进行定义的引用类型,其他类型实现接口,以保证它们支持某些操作.接口指定必须由类提供的成员或实现它的其他接口.与类相似,接口可以包含方法.属性.索引器和事件作为成员. 1.接口存 ...

  10. Apex语言(九)类的方法

    1.方法 方法是对象的行为.如下表: 看书,编程,打球就是方法. 2.创建方法 [格式] 访问修饰符 返回值类型 方法名(形式参数列表){ 方法体; } 访问修饰符:可以为类方法指定访问级别. 例如, ...