leetcode 之Permutation(七)
首先是next permutation的算法的描述和分析如下:


这题一是要知道思路,编程中注意STL的用法
void nextPermutaion(vector<int> &num)
{
next_permutation(num.begin(), num.end());
}
private:
template<typename BidiIt>
bool next_permutation(BidiIt first, BidiIt last)
{
//反向,注意!
auto rfirst = reverse_iterator<BidiIt>(last);
auto rlast = reverse_iterator<BidIt>(first);
auto pivot = next(rfirst);
while (pivot != rlast && *pivot > *prev(pivot))
pivot++; if (pivot == rlast)
{
reverse(rfist, rlast);
return false;
}
//注意用法
auto change = find_if(rfirst, pivot, bind1st(less<int>(), *pivot));
swap(*pivot, *change);
reverse(rfirst, pivot); return true;
}
接着是Permutation Sequence

有人用康托编码来解这个问题,不过个人觉得不太好理解,其实完全可以用上题的思路
string getPermutaion(int n, int k)
{
string s(n, '');
for (int i = ; i < n; i++)
s[i] += i + ; for (int i = ; i < k; i++)
next_permutation(s.begin(), s.end()); return s;
}
leetcode 之Permutation(七)的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- LeetCode Palindrome Permutation
原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Find Permutation 找全排列
By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...
- [leetcode]Next Permutation @ Python
原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...
随机推荐
- 聊聊flink Table的groupBy操作
本文主要研究一下flink Table的groupBy操作 Table.groupBy flink-table_2.11-1.7.0-sources.jar!/org/apache/flink/tab ...
- 【BZOJ2436】【NOI2011】NOI嘉年华(动态规划)
[BZOJ2436]NOI嘉年华(动态规划) 题面 BZOJ 题解 考虑第一问如何求解 发现状态与选择了哪些活动无关,只与时间有关 设\(f[i][j]\)表示前\(i\)个单位时间(离散后),一个嘉 ...
- BZOJ1089 [SCOI2003]严格n元树 【dp + 高精】
Description 如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树.如果该树中最底层的节点深度为d (根的深度为0),那么我们称它为一棵深度为d的严格n元树.例如,深度为2的严 ...
- WebLogic XMLDecoder反序列化漏洞(CVE-2017-10271)复现
WebLogic XMLDecoder反序列化漏洞(CVE-2017-10271) -----by ba ...
- 【Django实例】博客1
(上一篇) 一.概述 Blog是一个博客应用. dbe工程的目录结构,参考<序言>的最后部分.blog应用位于/home/russellluo/Django/dbe/dbe/blog目录下 ...
- BZOJ #3746: [POI2015]Czarnoksiężnicy okrągłego stołu 动态规划
转载请注明出处:http://www.cnblogs.com/TSHugh/p/8823423.html 读完题就会发现p=0.1的情况以及n=1.2的情况都可以直接判掉,而p=2的时候也可以直接构造 ...
- git 列出两个 commit 之间变更的文件列表
git diff <commit1> <commit2> --stat 如: git diff 74ecf17dc 1ee25ed3c --stat src/assets 上面 ...
- angular 有关侦测组件变化的 ChangeDetectorRef 对象
我们知道,如果我们绑定了组件数据到视图,例如使用 <p>{{content}}</p>,如果我们在组件中改变了content的值,那么视图也会更新为对应的值. angular ...
- python学习(十一)测试和调试
最近学习了python的错误处理和几种测试方法 1 try except 可以通过try except方式捕捉异常 try: print('try...') r = 10/0 print('resul ...
- 「PLC」PLC基本编程
PLC中无非就是三大量:开关量(数字量).模拟量.脉冲量.只在搞清楚三者之间的关系,你就能熟练的掌握PLC了. PLC编程算法(一) 1. 开关量也称逻辑量,指仅有两个取值,0或1.ON或OFF.它是 ...