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. [Apple开发者帐户帮助]四、管理密钥(2)获取密钥标识符

    创建JSON Web令牌(JWT)以与启用的服务进行通信时,需要密钥标识符. 获取密钥标识符 在“ 证书,标识符和配置文件”中,选择侧栏中“键”下的“全部”. 在右侧,选择私钥. 密钥标识符显示在密钥 ...

  2. 「LOJ10150」括号配对

    [题目] Hecy 又接了个新任务:BE 处理.BE 中有一类被称为 GBE. 以下是 GBE 的定义: 空表达式是 GBE 如果表达式 A 是 GBE,则 [A] 与 (A) 都是 GBE 如果 A ...

  3. 5.13会话技术Cookie---Session

    .会话技术: 1.会话:一次会话中包含多次请求和相应. 一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到有一方断开为止 2.功能:在一次会话的范围内的多次请求间,共享数据 3.方式: 1.客 ...

  4. Eclipse的菜单简介

    在Eclipse工作台的上方提供了菜单栏,该菜单栏包含了实现Eclipse各项功能的命令,并且与编辑器相关,即菜单栏中的菜单项与当前编辑器内打开的文件是关联的.例如,编辑器内没有打开任何文件,那么,将 ...

  5. VMWare虚拟机Centos 6.9中的 linux 配置静态ip地址上外网

    1.查看网络 # ifconfig 发现网络还没有配置,ping不通 2.修改网卡配置文件 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 添加如下配置 ...

  6. CDC之fast->slow (1)

    Sampling slower signals into faster clock domains causes fewer potential problems than sampling fast ...

  7. python 上手

    1.安装模块 cmd---“pip install [模块名]” 2.爬虫常用模块 requests beautifulsoup4 3.检查已安装的模块 cmd ---"pip list&q ...

  8. hadoop单机环境配置

    1.配置一台linux服务器(当前使用CentOS7)及一些基本设置 1.1在wmware下制作一台centos服务器(桥接模式)         并设置静态ip (编辑/etc/sysmconfig ...

  9. 【JSP】简单登陆界面

    学生登陆查询系统 1 程序的主要功能及特点 实现一个登录界面的基本功能,具体要求: 登录界面login.jsp含有表单,用户能够输入用户名和密码,并提交表单给verify.jsp. Verify.js ...

  10. Metadata

    元数据是关于数据的组织.数据域及其关系的信息,简言之,元数据就是关于数据的数据. Metadata is "data [information] that provides informat ...