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 ...
随机推荐
- 测试开发面试的Linux面试题总结之一:vim使用方法
现在做测试没有说不用到linux,大部分公司都会涉及到,作为测试经常使用linux最常见手段就是查看日志,帮助开发定位问题,这是目前最常见的测试当中使用linux方法,今天就讲一讲vim文本编辑器的使 ...
- jqury中关于ajax的几个常用的函数
一: AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术 ...
- Zookeeper(四)Hadoop HA高可用集群搭建
一.高可就集群搭建 1.集群规划 2.集群服务器准备 (1) 修改主机名(2) 修改 IP 地址(3) 添加主机名和 IP 映射(4) 同步服务器时间(5) 关闭防火墙(6) 配置免密登录(7) 安装 ...
- 【cdq分治】【CF1093E】 Intersection of Permutations
传送门 果然前两天写完咕咕咕那个题的题解以后博客就开始咕咕咕了-- Description 给定整数 \(n\) 和两个 \(1~\sim~n\) 的排列 \(A,B\). \(m\) 个操作,操作有 ...
- Codeforces 931.F Teodor is not a liar!
F. Teodor is not a liar! time limit per test 1 second memory limit per test 256 megabytes input stan ...
- LuaJavaBridge - lua与java互操作的简单解决方案
引入:Android平台代码和Lua代码的交互均通过C++和Java交互,Lua再和C++交互(lua <==> C++ <==> java) 我最开始遇见这种lua调用ja ...
- LINUX安全加固操作
1.禁止Ctrl-Alt-Delete组合键重启系统 vi /etc/inittab #ca::ctrlaltdel:/sbin/shutdown -t3 -r now 如果还存在下面的文件,则需要注 ...
- linux 文件IO
1.文件描述符 (1)文件描述符的本质是一个数字,这个数字本质上是进程表中文件描述符表的一个表项,进程通过文件描述符作为index去索引查表得到文件表指针,再间接访问得到这个文件对应的文件表.(2)文 ...
- c# extern 关键字
TEST.DLL 项目引用TEST.DLL 调用其中的方法 结果如下:
- Codechef Course Selection
Home » Practice(Hard) » Course Selection Course Selection Problem Code: RINSubmit https://www.codech ...