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 ...
随机推荐
- JBOSS内存溢出处理
JBOSS内存溢出处理 前几天公司一个项目的服务器坏了,就换了一个备份服务器顶替一下,但是没有跑一会就宕机了,一直报java.lang.OutOfMemoryError....一看到这里,就知道是内存 ...
- sql查询字段值的换行及回车符
SQL的换行.回车符,在MySQL.SQL Server和Oracle中均有不同,下面以列表显示. MySQL SQL Server Oracle 换行符 \n或\r\n或CHAR(10) CHA ...
- 调试EF源代码环境配置
下载EF6的源代码,运行build编译,Nuget会自动下载所需的DLL. 打开EF的工程,可以在EF解决方案下直接新建调试用的项目代码 添加EF引用时选择解决方案中的Entity Framework ...
- linux select
man select: #include <sys/select.h> #include <sys/time.h> int select(int nfds, fd_set *r ...
- Mac下无法拷贝文件到移动硬盘
Mac下无法拷贝文件到移动硬盘? 是移动硬盘的文件格式的问题. Mac系统无法识别 NTFS 格式的文件. 将移动硬盘格式化为 exFAT 格式的. 别担心,exFAT 格式的硬盘在Windows下也 ...
- [原]POJ-1631-Bridging signals-( 水LIS-O(nlogn) -DP)
题目大意:求最长上升子序列(LIS)长度,序列最大数不超过40000.因为只有上升排列的接口才不相交. 思路:普通的 O(n^2)的做法肯定会超时:因此,dp[ ] 记录长度为 i+1 的子序列中最末 ...
- 对Java“一切皆对象”的理念的理解
在从HelloWorld到面向对象中,我们将int, float, double, boolean等称为基本类型(primitive type),也就是特殊的类.我们可以将一个整数理解称为一个int类 ...
- leetcode:Happy Number
要求:Write an algorithm to determine if a number is "happy". A happy number is a number defi ...
- mysql中的去除空格函数
(1)mysql replace 函数 语法:replace(object,search,replace) 意思:把object中出现search的全部替换为replace 案例:update `ne ...
- 实际利率 > 名义利率
名义利率与实际利率的关系为:实际利率=(1+名义利率/计息周期)计息周期-1. 如计息周期数=1时,两者相等.如计息周期数大于1时,实际利率大于名义利率,当计息周期数小于1时,实际利率小于名义利率. ...