过程

从右往左,找到第一个A[i] < A[i+1];

从右往左,找到第一个A[j] > A[i], j > i;

交换A[i] 与 A[j];

将A[i + 1]之后的元素逆序(这里的i,j都是下标)。

代码

 class Solution {
public:
void nextPermutation(vector<int> &num) {
if(num.size() < )
return;
int i, j;
for(i = num.size() - ; i >= && num[i] >= num[i+]; --i) //第一个有序对
;
if(i < )  //已经是逆序
{
reverse(num.begin(), num.end());
return;
}
for(j = num.size() - ; num[j] <= num[i]; --j)   //第一个大于A[i]的
;
swap(num[i], num[j]);
reverse(num.begin() + i + , num.end());
}
};

STL-next permutation的更多相关文章

  1. STL之permutation/ equal_range/ binary_range学习

    1,is_permutation 函数,判断其中一个序列是不是另外一个序列的全排列. 包括四个参数,前两个是第一个数组需要判断的起始位置和终止位置.后两个是第二个数组需要判断的起始位置和终止位置. # ...

  2. [算法]——全排列(Permutation)以及next_permutation

    排列(Arrangement),简单讲是从N个不同元素中取出M个,按照一定顺序排成一列,通常用A(M,N)表示.当M=N时,称为全排列(Permutation).从数学角度讲,全排列的个数A(N,N) ...

  3. [Coding Study]——目录

    Coding Study Source Code for cnblogs This is the source code for coding study, you can see my Coding ...

  4. 【STL】next_permutation的原理和使用

    1.碰到next_permutation(permutation:序列的意思) 今天在TC上碰到一道简单题(SRM531 - Division Two - Level One),是求给定数组不按升序排 ...

  5. ACM: 强化训练-Inversion Sequence-线段树 or STL·vector

    Inversion Sequence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu D ...

  6. 组合数学 + STL --- 利用STL生成全排列

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  7. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

  8. LeetCode31 Next Permutation

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  9. UVA 10098 Generating Fast, Sorted Permutation

    // 给你字符串 按字典序输出所有排列// 要是每个字母都不同 可以直接dfs ^_^// 用前面说的生成排列算法 也可以直接 stl next_permutation #include <io ...

  10. Bridging signals(二分 二分+stl dp)

    欢迎参加——每周六晚的BestCoder(有米!) Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 6 ...

随机推荐

  1. SSIS ->> Script Debugging and Troubleshooting

    Breakpoint是调试过程中最重要的手段,不仅对于Script Task和Script Component,对于任何其他的组件也是如此.可以在某个Event(如OnError)触发的时候设置断点来 ...

  2. PHP 练习租房

    练习:租房子 <body> <form action="test.php" method="post"> <div>区域: ...

  3. pyhton与json,Xml

    对简单数据类型的encoding 和 decoding: 使用简单的json.dumps方法对简单数据类型进行编码,例如: 1 2 3 4 5 6 import json   obj = [[1,2, ...

  4. MDM平台学习笔记

    最近和将来一段时间都会花很多时间在主数据管理平台的学习和开发上,从现在开始打算记录此过程中的知识点和学习心得,加油! 1.IBM全新的产品文档网站IBM Knowledge Center,软件硬件产品 ...

  5. 2016MBA排名

    2016全球商学院100强 2016上半年度最受欢迎的十大MBA排名 网上评选出上年度最受欢迎的十大MBA排名,有你想要报考的院校吗?快来一睹这些MBA院校的风采,选择好适合自己的院校项目. 第1名 ...

  6. 第四讲:hibernate 的session (二)

    Session在hibernate中表示连接.下面介绍他的常用方法. 在使用前先把最重要的说了.打开API,发现在session中基本所有的方法(包括获得session的方法)都会抛出异常Hibern ...

  7. many to one could not resolve property

    今天在做一个功能的时候 遇到了.一个Could not resolve property 的问题. 配置文件如下: <many-to-one name="user"  cla ...

  8. Git server安装和配置

    yum install git yum install git-dameon toiseGit

  9. JSP:include的flush属性的作用

    JSP 中include 另一个文件时有个很偏的属性,叫flush,默认为 false.   在同一个 JSP 中,如果不断 include 自己(源文件),在逻辑上会形成死循环.若默认情况下,服务器 ...

  10. JDK,JRE,JVM区别与联系-理解与概括

    我们利用JDK(调用JAVA API)开发了属于我们自己的JAVA程序后,通过JDK中的编译程序(javac)将我们的文本java文件编译成JAVA字节码,在JRE上运行这些JAVA字节码,JVM解析 ...