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. Tortoise Git 安装 及报错处理

    TortoiseGit安装详解: https://www.cnblogs.com/xinlj/p/5978730.html Tortoise Git 错误处理 disconnected no supp ...

  2. forword动作

    forword动作   服务器内部跳转指令 语法为: <jsp:forword page = "目标页面"> 等同于:request.getRequestDispatc ...

  3. ccf算法模板

    bellman ford 算法求最短路径 #include <iostream> using namespace std; ; ; // 边, typedef struct Edge{ i ...

  4. c++ socket发送数据时,sendData = char * string 导致的乱码问题

    解决方法:将string 通过copy函数复制到某个char[] 1. string res =“xxx”; char arr[100]; int len = res.copy(arr, 100); ...

  5. hibernateHQL语句

    一.hql 1. 什么是hql HQL是Hibernate Query Language的缩写 查全部 2. hql和sql区别/异同 HQL SQL 类名/属性 表名/列名 区分大小写,关键字不区分 ...

  6. 异常过滤器的好坏(CLR)

    为什么有些语言支持它们而另一些不支持呢?把它们加到我的新语言里是个好主意吗?我应该什么时候使用过滤器和catch/rethrow?就像很多事情一样,异常过滤器有好的一面也有坏的一面… 什么是异常过滤器 ...

  7. mysql 查询账户

    查询 mysql 的存在的账户  >select user,host,password from mysql.user; # 可以查询涉及到user. host 链接权限.密码加密文件.

  8. 【JZOJ6206】【20190610】二分图边染色

    题目 ​ 对一个二分图的边染色,满足有相同端点的边的颜色一定不同; ​ 设最优染色为\(C\) ,你的染色为\(X\),只需要满足$ X \le 2^ {\lceil log  C \rceil }$ ...

  9. OPPO-Java面试-社招-一面(2019/07)

    个人情况 2017年毕业,普通本科,计算机科学与技术专业,毕业后在一个二三线小城市从事Java开发,2年Java开发经验.做过分布式开发,没有高并发的处理经验,平时做To G的项目居多.写下面经是希望 ...

  10. mysql sqrt() 函数

    mysql> ); +----------+ | sqrt() | +----------+ | | +----------+ row in set (0.00 sec)