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个排列组合即可,重点在于找出排序和k的规律。

参考:喜刷刷

Python:

class Solution(object):
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
seq, k, fact = "", k - 1, math.factorial(n - 1)
perm = [i for i in xrange(1, n + 1)]
for i in reversed(xrange(n)):
curr = perm[k / fact]
seq += str(curr)
perm.remove(curr)
if i > 0:
k %= fact
fact /= i
return seq 

C++:

class Solution {
public:
string getPermutation(int n, int k) {
string ret;
vector<int> factorial(n,1);
vector<char> num(n,1); for(int i=1; i<n; i++)
factorial[i] = factorial[i-1]*i; for(int i=0; i<n; i++)
num[i] = (i+1)+'0'; k--;
for(int i=n; i>=1; i--) {
int j = k/factorial[i-1];
k %= factorial[i-1];
ret.push_back(num[j]);
num.erase(num.begin()+j);
} return ret;
}
}; 

C++:

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

  

类似题目:

[LeetCode] 46. Permutations 全排列

[LeetCode] 47. Permutations II 全排列 II

[LeetCode] 31. Next Permutation 下一个排列

All LeetCode Questions List 题目汇总

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

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. [LeetCode] 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 60.Permutation Sequence (排列序列) 解题思路和方法

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

  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 60. Permutation Sequence(康托展开)

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

  6. 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(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  8. [LeetCode]60. Permutation Sequence求全排列第k个

    /* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...

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

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

随机推荐

  1. spriingboot使用thymeleaf

    1 添加jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  2. HDU4814——数学,模拟进制转换

    本题围绕:数学公式模拟进制转换 HDU4814 Golden Radio Base 题目描述 将一个十进制的非负整数转换成E(黄金分割数)进制的数 输入 不大于10^9的非负整数,处理到文件尾 输出 ...

  3. python正则表达式(1)--特殊字符

    正则表达式—特殊表达式含义 正则表达式的字母和数字表示他们自身,但多数字母和数字前加一个反斜杠时会拥有不同的含义. 下面列出了正则表达式模式语法中的特殊元素. 1.普通字符集 1)    \w     ...

  4. 运行pyqt4生成py文件增加代码

    if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) Form = QtGui.QWid ...

  5. Centos7 虚拟机里查看IP并启动网卡

    输入ip查询命名 ip addr  也可以输入 ifconfig(centOs7没有ifconfig命令)查看ip,但此命令会出现3个条目,centos的ip地址是ens33条目中的inet值. 发现 ...

  6. junit3和junit4的区别总结

    先来看一个例子: 先用junit3来写测试用例,如下: junit3测试结果: 从上面可看出: 1.junit3必须要继承TestCase类 2.每次执行一个测试用例前,junit3执行一遍setup ...

  7. Tensorflow细节-P309-高维向量可视化

    import matplotlib.pyplot as plt import tensorflow as tf import numpy as np import os from tensorflow ...

  8. php之大文件分段上传、断点续传

    前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...

  9. Minidumps 和 modules匹配

    简介 调试应用程序时,调试器必须加载可执行模块的符号,以便能够显示有意义的调用堆栈.当前源代码行.变量值等.如果您曾经调试过在另一个系统上创建的小型转储,那么您已经知道除了符号之外,调试器还需要访问创 ...

  10. 深搜的剪枝技巧(三)——Sticks(可行性剪枝、上下界剪枝、最优性剪枝)

    小木棍(最优性剪枝.可行性剪枝) 一.问题描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,已知每段的长都不超过 50 .现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍 ...