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. 论文翻译:BinaryConnect: Training Deep Neural Networks with binary weights during propagations

    目录 摘要 1.引言 2.BinaryConnect 2.1 +1 or -1 2.2确定性与随机性二值化 2.3 Propagations vs updates 2.4 Clipping 2.5 A ...

  2. mysql数据库 表 导入导出

    1.导出表结构 mysqldump --no-data -h192.168.222.11 -uroot -proot --databases db01 db02 db30>file.sql 2. ...

  3. 014_IP专项研究监控

    一.数据demo cat /proc/net/snmp Ip: Forwarding DefaultTTL InReceives InHdrErrors InAddrErrors ForwDatagr ...

  4. windows2012服务器中安装php7+mysql5.7+apache2.4环境

    1.下载安装apache.2.4 https://home.apache.org/~steffenal/VC14/binaries/httpd-2.4.38-win64-VC14.zip 解压到d盘的 ...

  5. mysql-数据(记录)相关操作(增删改查)及权限管理

    一.介绍 在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据的删除 使用SELECT查 ...

  6. face++ php

    总流程是先上传文件,保存到后台,获取返回来的face_token保存下来,然后拿face_token添加到faceSet里面去,搜索的时候结果会返回faceSet里面的face_token 1.dem ...

  7. html获取输入框的值

    https://zhinan.sogou.com/guide/detail/?id=316512383339

  8. 使用token做认证

    对当前用户,使用base64加密token,再解密token,但是不如JWT加密安全 import time import base64 import hmac def generate_token( ...

  9. 2018-2019-2 20165206 网络攻防技术 Exp5 MSF基础应用

    - 2018-2019-2 20165206<网络攻防技术>Exp5 MSF基础应用 - 实验任务 1.1一个主动攻击实践,如ms08_067; (1分) 1.2 一个针对浏览器的攻击,如 ...

  10. centos6.9 改系统语言成中文简体

    1.在root权限下 切换到root下:su root 查看当前语言环境:locale -a  (注意中间有空格) 如果看到 zh_CN.UTF-8(这个是中文简体)说明你的系统支持中文语言 2.编辑 ...