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 ...
随机推荐
- html 用图片代替重置按钮
提交时,若把按钮设置成图片提交特别方便只要 <input type="image" alt="点此提交" src="images/button. ...
- OOP数据库操作方法
一.数据库操作 连接MYSQL数据 面向对象访问数据库e.g. 造对象 $dx=new MySQLi("localhost","root","123& ...
- js dom
JavaScript的DOM操作 1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Window对象操作 一.属性和方 ...
- POJ 1568 Find the Winning Move(极大极小搜索)
题目链接:http://poj.org/problem?id=1568 题意:给出一个4*4的棋盘,x和o两人轮流放.先放够连续四个的赢.给定一个局面,下一个轮到x放.问x是否有必胜策略?若有,输出能 ...
- 《OD学hive》第五周0723
https://cwiki.apache.org/confluence/display/Hive/LanguageManual 一.创建表 create table student(id int, n ...
- Android ListView不响应OnItemClickListener解决办法
有时候,当ListView中的每一个item是自定义的View时,有可能会导致ListView的OnItemClickListener的listener无法调用,请看如下情况: 如果你的自定义List ...
- Tyvj 1085 派对
这道题和HDU 1016的素数环那道题很相似. 虽然1A了,但写代码的过程中还是丢三落四的. 贴完代码闪人,嘿嘿 //#define LOCAL #include <iostream> # ...
- UVa 101 The Blocks Problem
题意:给出从左到右放置的n块木块(从0开始编号),再给出四种操作,再给出相应的操作,输出操作结束后每一堆木块的情况. 学习的紫书,因为每一堆的木块数是在发生变化的,所以用vector. 然后就是模拟几 ...
- 新版Windows Azure CDN管理门户正式上线
经过产品团队的不懈努力,新版Windows Azure CDN管理门户在经过了有限开放预览之后,已经正式上线并开放给所有用户. 新版Windows Azure CDN管理门户经过全新的设计,除了在使用 ...
- DelegatingFilterProxy
安全过滤器链 Spring Security的web架构是完全基于标准的servlet过滤器的. 它没有在内部使用servlet或任何其他基于servlet的框架(比如spring mvc), 所以它 ...