LeetCode OJ 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个排列的最高位,等于这个数列中的第ceil(k / (n - 1)!)位.
下面是AC的代码:
class Solution {
public:
long long fac(int n){
int ret = 1;
while(n > 1){
ret *= n;
n--;
}
return ret;
}
string getPermutation(int n, int k) {
long long total = fac(n);
vector<int> left;
string ans;
int length, temp, digit;
for(int i = 1; i <= n; i++){
left.push_back(i);
}
for(int i = n; i > 0; i--){
total /= i;
temp = k / total;
if(k != temp * total){
digit = left[temp];
left.erase(left.begin() + temp);
}
else{
digit = left[temp - 1];
left.erase(left.begin() + temp - 1);
}
ans = ans + to_string(digit);
if(k != temp * total){
k -= temp * total;
}
else{
k -= (temp - 1) * total;
}
}
return ans;
}
};
112
LeetCode OJ 60. Permutation Sequence的更多相关文章
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- [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 ...
- 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 ...
随机推荐
- 关于daterangepicker的配置
一开始接触daterangepicker搞得思路很乱,慢慢研究才了解一些,下面粘一个daterangepicker的基本配置,代码是来自网上某位大神的.我只是引荐过来的,加入了周和月的汉化. 在回调函 ...
- tf.nn.dynamic_rnn
tf.nn.dynamic_rnn(cell,inputs,sequence_length=None, initial_state=None,dtype=None, parallel_iteratio ...
- python中编码问题
各种编码在内存中所占的大小: ascii: 英文:8bit (1B) uft-: 英文:8bit (1B) 中文:24bit (3B) GBK: 英文:8bit (1B) 中文:16bit (2B) ...
- centos 7 安装sqoop 1.4.7
1. 下载sqoop1.4.7 cd /home/workspace wget https://mirrors.tuna.tsinghua.edu.cn/apache/sqoop/1.4.7/sqoo ...
- Windows安装部署Tensorflow过程中的问题处理
1 在配置Tensorflow过程中有一行命令是 python object_detection/builders/model_builder_test.py 过程中出现了 NameError: na ...
- 网络爬虫2:使用crawler4j爬取网络内容
https://github.com/yasserg/crawler4j 需要两个包: crawler4j-4.1-jar-with-dependencies.jar slf4j-simple-1.7 ...
- 【LeetCode】2. Add Two Numbers 两数相加
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- Centos7使用pxe安装KVM虚拟机
Centos7使用pxe安装KVM虚拟机 一.安装服务所需的软件 [root@localhost ~]yum install nginx dhcp vsftpd syslinux -y [root@l ...
- 《Linux 性能及调优指南》3.1 确认瓶颈
翻译:飞哥 ( http://hi.baidu.com/imlidapeng ) 版权所有,尊重他人劳动成果,转载时请注明作者和原始出处及本声明. 原文名称:<Linux Performance ...
- 认识下java注解的实现原理
1,什么是注解 注解也叫元数据,例如常见的@Override和@Deprecated,注解是JDK1.5版本开始引入的一个特性,用于对代码进行说明,可以对包.类.接口.字段.方法参数.局部变量等进行注 ...