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.

这道题是让求出n个数字的第k个排列组合,由于其特殊性,我们不用将所有的排列组合的情况都求出来,然后返回其第k个,我们可以只求出第k个排列组合即可,那么难点就在于如何知道数字的排列顺序,可参见网友喜刷刷的博客,首先我们要知道当n = 3时,其排列组合共有3! = 6种,当n = 4时,其排列组合共有4! = 24种,我们就以n = 4, k = 17的情况来分析,所有排列组合情况如下:

1234
1243
1324
1342
1423
1432
2134
2143
2314 
2341
2413
2431
3124
3142
3214
3241
3412 <--- k = 17
3421
4123
4132
4213
4231
4312
4321

我们可以发现,每一位上1,2,3,4分别都出现了6次,当第一位上的数字确定了,后面三位上每个数字都出现了2次,当第二位也确定了,后面的数字都只出现了1次,当第三位确定了,那么第四位上的数字也只能出现一次,那么下面我们来看k = 17这种情况的每位数字如何确定,由于k = 17是转化为数组下标为16:

最高位可取1,2,3,4中的一个,每个数字出现3!= 6次,所以k = 16的第一位数字的下标为16 / 6 = 2,即3被取出
第二位此时从1,2,4中取一个,k = 16是此时的k' = 16 % (3!) = 4,而剩下的每个数字出现2!= 2次,所以第二数字的下标为4 / 2 = 2,即4被取出
第三位此时从1,2中去一个,k' = 4是此时的k'' = 4 % (2!) = 0,而剩下的每个数字出现1!= 1次,所以第三个数字的下标为 0 / 1 = 0,即1被取出
第四位是从2中取一个,k'' = 0是此时的k''' = 0 % (1!) = 0,而剩下的每个数字出现0!= 1次,所以第四个数字的下标为0 / 1= 0,即2被取出

那么我们就可以找出规律了
a1 = k / (n - 1)!
k1 = k

a2 = k1 / (n - 2)!
k2 = k1 % (n - 2)!
...

an-1 = kn-2 / 1!
kn-1 = kn-2 / 1!

an = kn-1 / 0!
kn = kn-1 % 0!

代码如下:

class Solution {
public:
string getPermutation(int n, int k) {
string res;
string num = "";
vector<int> f(n, );
for (int i = ; i < n; ++i) f[i] = f[i - ] * i;
--k;
for (int i = n; i >= ; --i) {
int j = k / f[i - ];
k %= f[i - ];
res.push_back(num[j]);
num.erase(j, );
}
return res;
}
};

转自:https://www.cnblogs.com/grandyang/p/4358678.html

Permutation Sequence 序列排序的更多相关文章

  1. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

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

  3. 【LeetCode每天一题】Permutation Sequence(排列序列)

    The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...

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

  5. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

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

  6. Next Permutation&&Permutation Sequence

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

  7. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. 60. Permutation Sequence

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  9. [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

随机推荐

  1. 反恐24小时第一季/全集24 Live Another Day迅雷下载

    反恐24小时 第一至九季 24 Season 1-9 (2001-2014) 本季看点:<24小时>第8季将在拥有美国的象征自由女神像的纽约开始,在新的一天,CTU重新开张,新的领导为从M ...

  2. 自己用图片做的可旋转、不确定进度的ProgressBar

    今天看到一个人将图片写个动画,然后就当做progressbar用了,思路挺棒的. 原文地址:http://blog.csdn.net/sweetvvck/article/details/2388253 ...

  3. java 生成zip文件并导出

    总结一下,关于Java下载zip文件并导出的方法,浏览器导出. String downloadName = "下载文件名称.zip"; downloadName = Browser ...

  4. Latex:多个公式使用同一个编号(右对齐)

    \usepackage{amsmath} \begin{equation}\begin{aligned}equation 1 ... \\equation 2 ... \\equation 3 ... ...

  5. java将XML文档转换成json格式数据

    功能 将xml文档转换成json格式数据 说明 依赖包:1. jdom-2.0.2.jar : xml解析工具包;2. fastjson-1.1.36.jar : 阿里巴巴研发的高性能json工具包 ...

  6. emouse思·睿—评论与观点整理之五

    虽说我主要做的硬件,平时的兴趣爱好比较关注移动互联网,混迹于虎嗅.爱范儿.雷锋网.36Kr.cnBeta.瘾科技.i黑马.TechWeb等这类科技以及创业媒体,遗憾的是系统的去写的并不多,好在还算充分 ...

  7. 很漂亮的Wince开发控件库Beemobile简介【转】

    https://blog.csdn.net/xiaoyong_net/article/details/80467653 官网地址:http://beemobile4.net/products/ipac ...

  8. C# winform DevExpress上传图片到数据库【转】

    实现功能如下图: 注明:此文使用的是DevExpress控件,winform 原生控件也是一样使用方法. 1.点击选择图片按钮,功能为通过对话框选择要上传的文件,并将该文件在下面的PictureEdi ...

  9. 初识C#程序结构

    一.编写第一个C#程序.注释:   1.编写程序   static void Main(string[] args)//在Mian方法下编写代码输出数据         {             C ...

  10. iOS中的多线程 NSOperation

    在ios中,使用多线程有三种方式,分别是:NSThread.NSOperation和NSOperationQueue.GCD,在本节,主要讲解一下NSOperation的使用. NSOperation ...