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. MyBatis3

    一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: <?xml version="1 ...

  2. L1-Day9

    1.学习让我感觉很棒.(什么关系?动作 or 描述?主语部分是?)         [我的翻译]Learning makes me that feel good.         [标准答案]Lear ...

  3. L1-Day8

    1.他就是我昨天见的那个人. [我的翻译]He is a man who I saw him yestorday. [标准答案]He is the man (who(m) /that) I saw y ...

  4. 7、字典和string的用法

    #dic={1:'alex','age':35,'hobby':{'girl_name':'铁锤','age':45},'is_handsome':True} # dic={'age':'alex', ...

  5. Pytorch学习笔记(一)---- 基础语法

    书上内容太多太杂,看完容易忘记,特此记录方便日后查看,所有基础语法以代码形式呈现,代码和注释均来源与书本和案例的整理. # -*- coding: utf-8 -*- # All codes and ...

  6. 论文笔记:Mask R-CNN

    之前在一次组会上,师弟诉苦说他用 UNet 处理一个病灶分割的任务,但效果极差,我看了他的数据后发现,那些病灶区域比起整张图而言非常的小,而 UNet 采用的损失函数通常是逐像素的分类损失,如此一来, ...

  7. babel-polyfill的几种使用方式

    前言 preset与plugin的关系: preset中已经包含了一组用来转换ES6+的语法的插件,如果只使用少数新特性而非大多数新特性,可以不使用preset而只使用对应的转换插件 babel默认只 ...

  8. linux服务器内存、根目录使用率、某进程的监控告警脚本

    脚本内容如下 #!/bin/bash #磁盘超过百分之80发送邮件告警 DISK_USED=`df -T |sed -n "2p" |awk '{print ($4/$3)*100 ...

  9. Linux定时执行PHP

    1.使用crond服务 crontab -e #编辑任务列表 crontab -l #展示任务列表 26 15 * * * /usr/local/php70/bin/php -q /data/www/ ...

  10. C#QQ邮箱验证

    注意: QQ邮箱的简单邮件传输协议(SMTP)使用了SSL加密,必须启用SSL加密.指定端口. QQ邮箱POP3/SMTP服务默认是关闭的,需要开启服务(设置=>账户=>开启服务). QQ ...