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. 417 事件、监听、jQuery、轮播手动

    am:通用事件 a链接事件阻止默认行为 return false HTML元素大都包含了自己的默认行为,例如:超链接.提交按钮等.我们可以通过在绑定事件中加上return false来阻止它的默认行为 ...

  2. SVM小白教程(2):拉格朗日对偶

    在上一篇文章中,我们推导出了 SVM 的目标函数: \[ \underset{(\mathbf{w},b)}{\operatorname{min}} ||\mathbf{w}|| \\ \operat ...

  3. 找到多个与名为“Home”的控制器匹配的类型。解决方法

    “/”应用程序中的服务器错误. 找到多个与名为“Home”的控制器匹配的类型.如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配 ...

  4. JMM以及并发三大特性介绍(包括解决方案)

    JMM结构图: JMM对同步的8种操作: JMM的同步规则: Countdownlatch介绍: 该类功能是可以阻塞线程,并在保证线程满足特定条件下,继续执行.如上图,Countdownlatch的c ...

  5. Freemarker网页静态化

    Freemarker的jar包 创建整合spring的配置文件 <?xml version="1.0" encoding="UTF-8"?> < ...

  6. tensorflow从入门到放弃-0

    刚接触tensorflow一周,感觉还是有点难度的.遇到这么个问题 failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable devic ...

  7. 解决SecureCRT中文版"数据库里没找到防火墙'无'"的错误提示

    问题描述: 最近从同事那拷贝到一个中文版的SecureCRT,但是每次打开都会有个防火墙的错误提示,“数据库里没找到防火墙“无”.此会话将尝试不通过防火墙进行连接. 出现这个错误的原因是在Secure ...

  8. IntelliJ IDEA安装ideaIU-2019.1

  9. ProgressDialog替代

    在API level 26 中,ProgressDialog被声明不赞成使用,应使用的替代方法是ProgressBar 利用下列代码实现ProgressBar的出现和消失 progressBar.se ...

  10. 拦截请求并记录相应信息-springboot

    方式: 1.FIlter过滤器 2.interceptor拦截器 3.Aspect切片 一.Filter过滤器形式 只能处理request中的数据  不能确定请求要走的是哪个controller信息 ...