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 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.
  • Given k will be between 1 and n! inclusive.

Example 1:

Input: n = 3, k = 3
Output: "213"

Example 2:

Input: n = 4, k = 9
Output: "2314"

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

34
43
24
42
23
32
34
43
14
41
13
31
24
42
14
41
12 <--- k = 17
21
23
32
13
31
12
21

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

最高位可取 1,2,3,4 中的一个,每个数字出现 3!= 6 次(因为当最高位确定了,后面三位可以任意排列,所以是 3!,那么最高位的数字就会重复 3!次),所以 k = 16 的第一位数字的下标为 16 / 6 = 2,在 "1234" 中即3被取出。这里的k是要求的坐标为k的全排列序列,定义 k' 为当最高位确定后,要求的全排序列在新范围中的位置,同理,k'' 为当第二高为确定后,所要求的全排列序列在新范围中的位置,以此类推,下面来具体看看:

第二位此时从 1,2,4 中取一个,k = 16,则此时的 k' = 16 % (3!) = 4,注意思考这里为何要取余,如果对这 24 个数以6个一组来分,那么 k=16 这个位置就是在第三组(k/6 = 2)中的第五个(k%6 = 4)数字。如下所示,而剩下的每个数字出现 2!= 2 次,所以第二数字的下标为 4 / 2 = 2,在 "124" 中即4被取出。

24
42
14
41
12 <--- k' = 4
21

第三位此时从 1,2 中去一个,k' = 4,则此时的 k'' = 4 % (2!) = 0,如下所示,而剩下的每个数字出现 1!= 1 次,所以第三个数字的下标为 0 / 1 = 0,在 "12" 中即1被取出。

12 <--- k'' = 0
21

第四位是从2中取一个,k'' = 0,则此时的 k''' = 0 % (1!) = 0,如下所示,而剩下的每个数字出现 0!= 1 次,所以第四个数字的下标为 0 / 1= 0,在 "2" 中即2被取出。

12 <--- k''' = 0

那么就可以找出规律了
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;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/60

类似题目:

Next Permutation

Permutations

参考资料:

https://leetcode.com/problems/permutation-sequence/

https://leetcode.com/problems/permutation-sequence/discuss/22508/An-iterative-solution-for-reference

LeetCode All in One 题目讲解汇总(持续更新中...)

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

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

  2. 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 @ Python

    原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...

  4. LeetCode: Permutation Sequence 解题报告

    Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...

  5. LeetCode——Permutation Sequence

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

  6. [Leetcode] Permutation Sequence

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

  7. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

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

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

随机推荐

  1. 挣值管理 EVM

    项目的挣值管理(Earned Value Management,EVM),是用与进度计划.成本预算和实际成本相联系的三个独立的变量,进行项目绩效测量的一种方法. 有三个比较重要的参数,用这三个参数能够 ...

  2. 在Azure上的VM镜像库中找到想要的镜像

    Azure上的虚机镜像库中, 有很多的镜像,其中当然也包括了用户自定义上传的镜像. 在Powershell中如果想使用这些镜像的话, 则需要知道其名称 下面这条命令,可以获得所有的镜像信息 $imag ...

  3. python之路目录

    python开发[第一篇]:目录 python开发[第二篇]:python初体验 python开发[第三篇]:python基础之条件控制与循环 python开发[第四篇]:python基础之运算符 p ...

  4. JAVA调用R

    JAVA很适合开发应用系统,但是数学建模和计算能力非其所长,如果该系统需要进行大量的统计或者优化的计算,调用R是一种很好的方式.JAVA负责系统的构建,R用来做运算引擎,从而实现应用型和分析性相结合的 ...

  5. windows 7下React Native环境配置

    React Native 是 Facebook 推出的一个用 Java 语言就能同时编写 ios,android,以及后台的一项技术,它可以做到实时热更新 .FaceBook 也号称这们技术是 “Le ...

  6. Android中通信协议

    一.TCP/IP协议(传输层协议) 1.Socket与ServerSocket Socket是应用层与TCP/IP协议簇通讯的中间抽象层,Socket是一组接口,在设计模式中,Socket的设计就是门 ...

  7. C#使用ADO.NET访问数据库(一)

    博主好久没更新博客了,最近有点忙(打麻将0.0..),今天更新一篇C#的,我还是想坚持更新博客,分享一下自己的心得,闲话少说,开始正题~~ ADO.NET概述:ADO.NET的作用在于他是客户端访问服 ...

  8. ERR_CONTENT_DECODING_FAILED错误的原因和解决办法

    1. ERR_CONTENT_DECODING_FAILED错误的原因 这种错误通常发生于Http请求中的头部信息标识内容是gzip编码的,但实际上不是. 2. ERR_CONTENT_DECODIN ...

  9. IntelliJ IDEA安装及jsp开发环境搭建

    一.前言 现在.net国内市场不怎么好,公司整个.net组技术转型,就个人来说还是更喜欢.net,毕竟不是什么公司都像微软一样财大气粗开发出VS这样的宇宙级IDE供开发者使用,双击sln即可打开项目, ...

  10. 在DBeaver中phoenix查询报错:org.apache.phoenix.exception.PhoenixIOException: The system cannot find the path specified

    环境:Phoenix:4.4,win7系统 问题:Phoenix在查询hbase时,报"系统找不到指定路径". 解决: 请参见 https://distcp.quora.com/C ...