LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++>

LeetCode 31 Next Permutation

给出一个序列,求其下一个排列

STL中有std::next_permutation这个方法可以直接拿来用

也可以写一个实现程序:

  1. 从右往左遍历序列,找到第一个nums[i-1]<num[i]的位置,记p = i-1
  2. 如果第一步没有找到,说明整个序列满足单调递减,也就是最大的排列,那么倒置序列,return即可。
  3. 再次从右往左遍历序列,找到第一个nums[i]>nums[p]的位置,std::swap(nums[i],nums[p])
  4. 此时从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]的更多相关文章

  1. LeetCode 31. 下一个排列(Next Permutation)

    题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常 ...

  2. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  3. LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会 ...

  4. [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 ...

  5. [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 ...

  6. [LeetCode] 31. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

  8. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

  9. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

随机推荐

  1. rootkit后门检测工具

    1. 关于rootkit rootkit是Linux平台下最常见的一种木马后门工具,它主要通过替换系统文件来达到入侵和和隐蔽的目的,这种木马比普通木马后门更加危险和隐蔽,普通的检测工具和检查手段很难发 ...

  2. IDEA - Debug - not supported in -source 1.5

  3. Pycharm问题:module 'pip' has no attribute 'main'

    更新pip之后(pip 10 版本之后),Pycharm安装package出现报错:module 'pip' has no attribute 'main' 解决办法如下: 找到Pycharm安装目录 ...

  4. docker安装小笔记

    作者:邓聪聪 yum update Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker docker卸载旧版本(如 ...

  5. java学习笔记07-循环

    java有三种主要的循环结构 while循环 do...while循环 for循环 while循环 while(布尔表达式){ //循环内容 } public static void main(Str ...

  6. python基础--numpy.dot

    # *_*coding:utf-8 *_* # athor:auto import numpy dot = numpy.dot([0.100, 0.200],2.) print(dot) #[ 0.2 ...

  7. python正则表达式--split、sub、escape方法

    1.re.split 语法: re.split(pattern, string[, maxsplit=0, flags=0]) 参数: pattern    匹配的正则表达式 string      ...

  8. linux yum提示Loaded plugins: fastestmirror, security错误的解决方法

    别听网上的,先检查自己是不是打错了.........我就是打错了一个横杆搞了一个多小时 具体: 如图这种,长横杆 修改后结果: 所以在不知情的情况之下千万不要乱改东西,先检查代码是否有误 题外话: = ...

  9. Django—常用功能

    索引 一.静态文件 二.中间件 三.Admin站点 3.1 列表页选项 3.2 编辑页选项 3.3 重写模板 四.上传图片 4.1 在管理页面admin中上传图片 4.2 自定义form表单中上传图片 ...

  10. python数据类型之基础进阶

    一: 解构 1.1 结构字符串 变量和字符个数必须严格一致 name = 'wc' a,b=name print(a) print(b) # w # c name = 'w' a,b=name pri ...