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 ...
随机推荐
- RHEL7安装配置VNC
RHEL7安装配置VNC 作者:Eric 微信:loveoracle11g 安装配置VNC服务程序 [root@zhouwanchun yum.repos.d]# cd ~ [root@zhouwan ...
- java读取按行txt文件
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; pub ...
- Nginx 配置为https服务器
本机作为https服务器 server { listen ssl; server_name localhost; ssl_certificate ssl/server.crt; ssl_certifi ...
- js原生面向对象-仿layui选项卡
喜欢琢磨,给大家分享小编自己封装的仿layui的选项卡. <!DOCTYPE html> <html lang="en"> <head> < ...
- 腾讯微信被怼,iOS版微信不能打赏了
2017年4月19日,估计很多有着大量粉丝的微信自媒体作者会感到很不爽,因为他们的苹果粉丝再也无法很爽快地.肆意.任性地打赏他们了,按目前iphone手机的占有率,估计打赏率会掉一半以上. 据微信派微 ...
- time random sys os 模块
时间模块 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: (1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日 ...
- SQL with(unlock)与with(readpast)
所有Select加 With (NoLock)解决阻塞死锁,在查询语句中使用 NOLOCK 和 READPAST 处理一个数据库死锁的异常时候,其中一个建议就是使用 NOLOCK 或者 READPAS ...
- Phpstorm的强大功能
你是否也是重复,重复,一直重复的打着重复的代码? 你是否也一直重复重复 的 Ctrl+c .Ctrl+v? 当你看到此项操作的时候就会发现,原来复制粘贴还能这么玩儿... 演示效果(以phpExcel ...
- official shiro(Reference Manual)
Apache Shiro Reference Documentation Overview Core Spring-based Applications 1.Overview pom.xml < ...
- [Unity插件]Lua行为树(一):BehaviorDesigner源码分析
BehaviorDesigner是Unity上的一款行为树插件,不过这个插件是用C#编写的,编写出来的行为树也是依赖于C#的,不利于热更,所以有必要写一个lua版本的. 首先下载BehaviorDes ...