LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++>
LeetCode 31 Next Permutation
给出一个序列,求其下一个排列
STL中有std::next_permutation这个方法可以直接拿来用
也可以写一个实现程序:
- 从右往左遍历序列,找到第一个
nums[i-1]<num[i]的位置,记p = i-1。 - 如果第一步没有找到,说明整个序列满足单调递减,也就是最大的排列,那么倒置序列,
return即可。 - 再次从右往左遍历序列,找到第一个
nums[i]>nums[p]的位置,std::swap(nums[i],nums[p])。 - 此时从
p位置开始到序列最右端一定满足单调递减,倒置这一部分,使其字典序最小,所得序列即为下一个排列。
class Solution {
public:
void nextPermutation(std::vector<int>& nums) {
int p = -1;
for(int i = nums.size()-1; i>=0; i--)
if(i-1>=0 && nums[i]>nums[i-1]){
p = i-1;
break;
}
if(p==-1){
std::reverse(nums.begin(),nums.end());
return;
}
for(int i = nums.size()-1; i>=0; i--)
if(nums[i]>nums[p]){
std::swap(nums[i],nums[p]);
break;
}
std::reverse(nums.begin()+p+1,nums.end());
}
};
LeetCode 60 Permutation Sequence
求长度为n的序列(1~n)全排列的第k大排列
如果不停调用std::next_permutation肯定会超时。
利用康托展开,所有排列按照字典序排序后,按顺序编号,称为康托编码
那么根据序列长度和编号求解序列,实际就是解码过程。
编码解码详见 https://blog.csdn.net/synapse7/article/details/16901489
class Solution {
public:
std::string getPermutation(int n, int k) {
std::string ans;
std::string seq0(n,'0');
for(int i = 0; i<n; i++){ // seq0: 1~n minimal permutation
seq0[i] += i+1;
}
int base = 1;
for(int i = 1; i<n; i++) base *= i;
k--;
for(int i = 0; i<n-1; k %= base, base/=n-i-1, i++){
auto pos = seq0.begin()+k/base;
ans.push_back(*pos);
seq0.erase(pos);
}
ans.push_back(seq0[0]);
return ans;
}
};
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]的更多相关文章
- LeetCode 31. 下一个排列(Next Permutation)
题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常 ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会 ...
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
随机推荐
- 在可编辑div的光标下插入html
function pasteHtmlAtCaret(html, selectPastedContent) {//参数1为要插入的html //参数2为boolean 是否选中插入的html 默认为fa ...
- 基于Python的Webservice开发(三)-Django安装配置
一.安装Django pip install django 二.创建项目 进入指定的目录后 django-admin startproject WebApi 目录说明: WebApi 项目的容器. m ...
- JustSoso笔记
当时想了大半天,想着到底要怎么绕过MD5呢,结果还是没做出来,即使问了学长,自己还是漏了一个步骤,file=hint.php,特此笔记,又学到了个引用变量的知识 学习自 https://www.ctf ...
- MySQL2.字符集乱码
MySQL2.字符集 此节记录下MySQL出现乱码的原因.还是参考小册子~ 字符集简介 计算机中只能存储二进制数据,建立字符与二进制数据的映射关系来存储字符. 从两方面考虑: 1.界定清楚字符范围,即 ...
- ext window嵌jsp页面自适应
//定义window调用方法传入jsp所需参数function getWindow(obj,obj1,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9){ Ext.def ...
- linux常见故障处理
目录 一. 文件和目录类 1.1 File exist 文件已经存在 1.2 No such file or directory 没有这个文件或目录(这个东西不存在) 1.3 command not ...
- JS学习过程中碰到的小问题
使用循环语句查找通讯录 //Setup var contacts = [ { "firstName": "Akira", "lastName" ...
- Unity中锚点的动态设置
问题背景 在做签到系统时,需求给的效果图如下 效果图像这样,中间是模型,周围其他是签到框这样的布局,我想动态生成各个动态框,涉及到一个定位问题,锚点的设置(动态去设置每个item的位置) 实现方法 S ...
- loadrunner中的web_convert_param函数
某些场景中获取的参数.自定义参数直接作用于请求的body或url时将不会被特殊的转换为页面编码一致的字符串,导致提交至服务的拼接字符串格式不正确,我们就可以将字符串转为url或html中的格式. 注: ...
- 论文阅读笔记五十五:DenseBox: Unifying Landmark Localization with End to End Object Detection(CVPR2015)
论文原址:https://arxiv.org/abs/1509.04874 github:https://github.com/CaptainEven/DenseBox 摘要 本文先提出了一个问题:如 ...