LeetCode60:Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
1 “123”
2 “132”
3 “213”
4 “231”
5 “312”
6 “321”
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
Backtracking Math
这道题最直观的的解法就是先求出全部的排列,然后再从结果中找到第k个值就可以。可是非常明显会超时。
假设不能先将全部的排列都求出来,那么这道题的目的就是让我们直接找到第k个排列了。
那么怎样找到第k个排列?直接要找到规律可能会比較困难,可是能够使用回溯和动态规划的一般方法,即使用用例来分析,从特殊到一般。看看通过这个特殊的用例能不能找到通用的方法,可是使用用例分析可能会因为用例选取的不全而导致遗漏一些情况,这道题做到最后就是用例选取的不全导致改了好久。
取n=3,k=5,那么输出应该是第5个排列”312”。
能够发现n=3时的全部排列中以1开头的排列有2个,以2开头的排列有2个,以3开头的排列有2个。
排列的个数取决于后面的数有多少种排列,这里后面有2个数,排列的个数是2!=2。
于是对于k=5能够这么分析
5/2=2;
5%2=1
即将[123]第0位的数字1和第2位的数字3交换,第0位就处理好了,如今数组变成[321],接着指针移到到第1位。然后将第1位到最后的元素排序。数组变成了[312],然后求[12]中的第1个数。
可是这样的求解方法会有一点问题,那就是本来5和6应该都是和第2位交换,可是因为6/2=3,结果变成了第0位和第3位交换,非常明显这是错误的,我们应该使用它在结果集中的下标来使用这个元素。对于k=5,实际上是第k-1=4个元素。对于4:
4/2=2;
4%2=0
它表示第0个元素要和第2个元素交换,这时第0个元素就处理好了,然后再在后面的2个元素构成的排列中查询第4%2=0个元素,当全部的元素都处理好了以后,这个数组中的元素就是我们要找的第k个排列了。
runtime:4ms
class Solution {
public:
string getPermutation(int n, int k) {
arr=new char[n];
for(int i=0;i<n;i++)
arr[i]=i+'1';
helper(0,n,k-1);
string str;
for(int i=0;i<n;i++)
str+=arr[i];
return str;
}
void helper(int pos,int num,int k)
{
if(pos==num-1)
return ;
int base=k/fac(num-pos-1);
int remain=k%fac(num-pos-1);
sort(arr+pos,arr+num);
swap(arr[pos],arr[pos+base]);
helper(pos+1,num,remain);
}
int fac(int n)
{
int result=1;
for(int i=1;i<=n;i++)
result*=i;
return result;
}
private:
char *arr;
};
LeetCode60:Permutation Sequence的更多相关文章
- LeetCode31 Next Permutation and LeetCode60 Permutation Sequence
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode60. Permutation Sequence第k个排列
给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...
- [Swift]LeetCode60. 第k个排列 | Permutation Sequence
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【leetcode】 Permutation Sequence (middle)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- [Leetcode] Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- 350 Intersection of Two Arrays II 两个数组的交集 II
给定两个数组,写一个方法来计算它们的交集.例如:给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].注意: 输出结果中每个元素出现的次数, ...
- drupal-使用hook_preprocess_field在paragraph的accordion中添加自定义数据
描述:我的accordion类型原先只有两个字段,分别是title和content.显示在页面上会默认隐藏其内容,点击“+”会显示内容.然而现在有一个新需求,就是加一个开关使编辑内容者可以选择默认“展 ...
- spring 将配置文件中的值注入 属性
1.编写配置文件 #债权转让 #默认周期 必须大于0 credit.defaultDuration=1 #最小转让金额(元) credit.minBidAmount=1.00 #最小转让时间 到期时间 ...
- Caffe2:python -m caffe2.python.operator_test.relu_op_test
1. 进行语句测试时候,出现问题, 设置环境变量CUDA_VISIBLE_DEVICES 参考: cuda设置指定可见方法 在/etc/profile文件或者-/.bashrc末尾添加以下行: exp ...
- UICollectionView(一)基本概念
整体预览 高等级的包含和管理(Top-level containment and management) UICollectionView UICollectionViewController UIC ...
- ubuntu 更改终端颜色
1.$ sudo gedit .bashrc 2.PS1="\[\033[1;36;01m\]\u\[\033[00m\]\[\033[1;34;01m\]@\[\033[00m\]\[\0 ...
- lsof command not found 解决
有些centos 没有 lsof命令,需要安装 yum install lsof -y 使用: lsof -i:端口号
- 【上海站】EOLINKER 用户培训之旅,等你来共建API新连接
从今年3月4日起,EOLINKER AMS 团队将再次开启全国用户培训之旅.本次全国培训之旅依旧将覆盖北上广深等国内主要城市,重点提供两种服务内容,一是 对 EOLINKER 产品的交流,包括 API ...
- git学习(1)
一.git fetch 和git pull 的差别 1.git fetch 相当于是从远程获取最新到本地,不会自动merge,如下指令: git fetch orgin master //将远程仓库的 ...
- sysbench基准测试工具
一.简介SysBench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况.当前功能允许测试的系统参数有:file I/O performance (文件I ...