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 ...
随机推荐
- MySQL常用语句大全
数据库操作:创建数据库create database database_name 查看数据库 show databases使用数据库use dbname删除数据库 drop database dbna ...
- 浅析Linux DeviceTree
文本将介绍Linux DeviceTree的相关知识,包括DeviceTree源文件.结构.语法.编写规则等. DeviceTree基础 DeviceTree(以下简称DT)用于描述设备信息以及设备于 ...
- 第7课 列表初始化(2)_分析initializer_list<T>的实现
1. 初始化列表的实现 (1)当编译器看到{t1,t2…tn}时便会生成一个initializer_list<T>对象(其中的T为元素的类型),它关联到一个array<T,n> ...
- dataset to list
http://www.c-sharpcorner.com/UploadFile/ee01e6/different-way-to-convert-datatable-to-list/ http://ww ...
- python基础知识-(1)语法基础知识总结(转载)
1.Python标识符 在 Python 里,标识符有字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大 ...
- MongoDB基础命令
MongoDB 入门命令 查看当前数据库 > show dbs admin 0.000GB config 0.000GB local 0.000GB > -- use databaseNa ...
- 学习js第二天小结
if-else if ---------适用于区间范围的判断 If(判断条件--一般是boolean类型的值或是关系表达式或是逻辑表达式 ){ 要执行的代码; }else if(判断条件) ...
- for循环中进行联网请求数据、for循环中进行异步数据操作,数据排序错乱问题解决;
for循环中进行联网请求数据,由于网络请求是异步的,第一个网络请求还没有回调,第二次第三次以及后续的网络请求又已经发出去了,有可能后续的网络请求会先回调:这时我们接收到的数据的排序就会错乱:怎么才能让 ...
- Solr字段类型field type的定义
摘要: Solr的字段类型定义了Solr如何解析字段数据并将数据检索出来,了解Solr的字段类型定义有助于更好的配置与使用Solr. 字段类型的定义 字段类型的定义主要包含如下四个方面的信息: 名称 ...
- oracle查看和替换含不可见字符(空白)
select lengthb('1397256'), dump('1397256') from dual; select ascii('') from dual; ), '') from dua ...