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 ...
随机推荐
- Zabbix概术及基础介绍(一)
一.Zabbix介绍 Zabbix 是由Alexei Vladishev创建,目前由Zabbix SIA在持续开发和支持.Zabbix 是一个企业级的分布式开源监控方案.Zabbix是一款能够监控各种 ...
- MyBatis openSession(),close(),和commit() 底层代码剖析
一:MyBatis工具类 中openSession到底做了什么? Mybatis工具类 private static final String RESOURCE = "mybatis-con ...
- 导入(移动)数据到hive1.1.0表的方法
hive数据导入代码格式(会移动源文件位置): LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [partit ...
- C++堆和栈详解(转)
一.预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其 操 ...
- CDOJ--1369
原题链接:http://acm.uestc.edu.cn/problem.php?pid=1369&cid=192 分析:DP,dp[i][0]表示第i个人不是lover时的最小值,dp[i] ...
- Hdu3022 Sum of Digits
Sum of Digits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- Ubuntu 使用Compiz配置炫酷3D桌面
先看一下效果 要实现这种3D 的效果其实很简单. Step 1:安装N卡驱动工具 sudo apt- 这个东西其实没有太大的作用. Step 2:安装Compiz sudo apt-get insta ...
- duilib 修复CTreeViewUI控件动态添加子控件时,对是否显示判断不足的bug
转载请说明出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42264947 这个bug我在仿酷狗开发日志里提到过,不过后来发现修复的不够 ...
- linux shell学习一
本博客参考自: http://www.cnblogs.com/waitig/p/5523409.html <shell从入门到精通> 张春晓编著 Shell简介 Shell自身是一个用C ...
- IIS7.5 HTTP 错误500.19-Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效
IIS7.5 HTTP 错误500.19-Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效 --------------------------------- ...