[LeetCode] 60. 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):
"123"
"132"
"213"
"231"
"312"
"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 序列排序的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [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 ...
- 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 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 60. Permutation Sequence(康托展开)
描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 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(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- [LeetCode]60. Permutation Sequence求全排列第k个
/* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
随机推荐
- VUE之路
最近研究了下Vue这个前端框架,不得不说这个前端框架很是厉害.不过对于习惯了jQuery的我来说,刚上手那会儿还是踩了很多的坑啊.那会儿觉得天啊,这个Vue框架特别的绕,并且也更复杂.不过待我写了几天 ...
- django-安装nginx及fastdfs-nginx-module
安装nginx及fastdfs-nginx-module 1. 解压缩 nginx-1.8.1.tar.gz 2. 解压缩 fastdfs-nginx-module-master.zip 3. 进入n ...
- 输入一个正整数n,生成一张2的乘方表,输出2*0—2*n的值。
#include<stdio.h>#include<math.h> //程序中调用幂函数pow(),需包含头文件math.h//void main(){ int i,n; pr ...
- Python爬虫 | 多线程、多进程、协程
对于操作系统来说,一个任务就是一个进程(Process),比如打开一个浏览器就是启动一个浏览器进程,打开一个记事本就启动了一个记事本进程,打开两个记事本就启动了两个记事本进程,打开一个Word就启动了 ...
- ArrayList 集合 简单运用
集合 遍历 import java.util.ArrayList; class Demo02 { public static void main(String[] args) { // 创建Arra ...
- sdcf day4 qaq模拟赛总结
目录 链接 总结 链接 点这里,O(∩_∩)O~ 总结 我还是太菜了,第二题提交了\(8\)遍,真心无语了 总结来看 一是思维能力弱,第二个思维题想了很长时间,想不出来 二是代码能力弱,尽管代码短,但 ...
- 【JZOJ6229】【20190621】san
题目 \(n\)个点\(m\)条边的有向图,每个点有点权 你可以选择拓扑序的一个区间的 最大化点权和 $n \le 50 , m \le \frac{n*(n-1)}{2} , 0 \le |a_i ...
- windows环境搭建dubbo服务
windows环境搭建dubbo服务 1:首先需要下载dubbo的注册中心 zookeeper zookeeper注册中心下载地址链接:http://mirror.bit.edu.cn/apache/ ...
- Linux crontab命令:循环执行定时任务(详解版)
前面学习了 at 命令,此命令在指定的时间仅能执行一次任务,但在实际工作中,系统的定时任务一般是需要重复执行的.而 at 命令显然无法满足需求,这是就需要使用 crontab 命令来执行循环定时任务. ...
- Hadoop综合大作业总评
作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3363 1.把python爬取的数据传到linux 2.把数据的逗号代替为 ...