STL-next permutation
过程:
从右往左,找到第一个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的更多相关文章
- STL之permutation/ equal_range/ binary_range学习
1,is_permutation 函数,判断其中一个序列是不是另外一个序列的全排列. 包括四个参数,前两个是第一个数组需要判断的起始位置和终止位置.后两个是第二个数组需要判断的起始位置和终止位置. # ...
- [算法]——全排列(Permutation)以及next_permutation
排列(Arrangement),简单讲是从N个不同元素中取出M个,按照一定顺序排成一列,通常用A(M,N)表示.当M=N时,称为全排列(Permutation).从数学角度讲,全排列的个数A(N,N) ...
- [Coding Study]——目录
Coding Study Source Code for cnblogs This is the source code for coding study, you can see my Coding ...
- 【STL】next_permutation的原理和使用
1.碰到next_permutation(permutation:序列的意思) 今天在TC上碰到一道简单题(SRM531 - Division Two - Level One),是求给定数组不按升序排 ...
- ACM: 强化训练-Inversion Sequence-线段树 or STL·vector
Inversion Sequence Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%lld & %llu D ...
- 组合数学 + STL --- 利用STL生成全排列
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
- LeetCode31 Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- UVA 10098 Generating Fast, Sorted Permutation
// 给你字符串 按字典序输出所有排列// 要是每个字母都不同 可以直接dfs ^_^// 用前面说的生成排列算法 也可以直接 stl next_permutation #include <io ...
- Bridging signals(二分 二分+stl dp)
欢迎参加——每周六晚的BestCoder(有米!) Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 6 ...
随机推荐
- [转]C:int型指针
开源中国:http://my.oschina.net/lotte1699/blog/142538 网页快照:http://www.piaocafe.com/295977937/139381567037 ...
- POJ 1743 Musical Theme(后缀数组)
题意:有n个数值,算出相邻两个值的差值,此时有n-1个值的序列,把这序列当做字符串的话,求最长重复子串,且这两个子串不能重叠. 分析:后缀数组解决.先二分答案,把题目变成判定性问题:判断是否存在两个长 ...
- C# 打印小票 POS
C# 打印小票 POS 最近在写一个餐饮的收银系统,以前从来没有碰过打印机这玩意.感觉有些无从下手,在前面做报表时,总想找第三方的控件来用用,结果始终不行没搞定.没研究透,催得急没办法还是的动手自己写 ...
- 如何把双引号包含到echo命令的字符串中
初涉s h e l l的用户常常会遇到的一个问题就是如何把双引号包含到e c h o命令的字符串中.引号是一个特殊字符,所以必须要使用反斜杠\来使s h e l l忽略它的特殊含义.假设你希望使用e ...
- windows下安装python,安装框架django。
第一步: 首先下载python安装包: 第二步:安装 双击安装包,安装程序. 这里安装到C盘 文件夹命名为 python33. 正在安装......... ...
- Getting Error "Invalid Argument to LOCATOR.CONTROL: ORG_LOCATOR_CONTROL='' in Material Requirements Form (文档 ID 1072379.1)
APPLIES TO: Oracle Work in Process - Version 11.5.10.2 and later Information in this document applie ...
- 安全删除mysql binlog日志
命令行下执行 show binary logs; purge binary logs to 'mysql-bin.000070';
- 不同浏览器JS获取浏览器高度和宽度
摘自:http://blog.csdn.net/lai_gb/archive/2009/07/04/4320956.aspx IE中: document.body.clientWidth ==> ...
- WCF常见异常-The maximum string content length quota (8192) has been exceeded while reading XML data
异常信息:The maximum string content length quota (8192) has been exceeded while reading XML data 问题:调用第三 ...
- HDU 1851 (巴什博奕 SG定理) A Simple Game
这是由n个巴什博奕的游戏合成的组合游戏. 对于一个有m个石子,每次至多取l个的巴什博奕,这个状态的SG函数值为m % (l + 1). 然后根据SG定理,合成游戏的SG函数就是各个子游戏SG函数值的异 ...