【LeetCode】060. 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.
题解:
Solution 1
class Solution {
public:
string getPermutation(int n, int k) {
string s;
for(int i = ; i < n; ++i){
s += (i + ) + '';
}
for(int i = ; i < k - ; ++i){
next_permutation(s);
}
return s;
}
void next_permutation(string &str){
int n = str.size();
for(int i = n - ; i >= ; --i){
if(str[i] >= str[i + ]) continue;
int j = n - ;
for(; j > i; --j) {
if(str[j] > str[i]) break;
}
swap(str[i], str[j]);
reverse(str.begin() + i + , str.end());
return;
}
reverse(str.begin(), str.end());
}
};
Solution 2
class Solution {
public:
string getPermutation(int n, int k) {
string res;
if(n <= || k <= ){
return res;
}
string num = "";
vector<int> f(n, );
for(int i = ; i < n; ++i){
f[i] = f[i - ] * i;
}
--k;
for(int i = n; i > ; --i){
int j = k / f[i - ];
k %= f[i - ];
res.push_back(num[j]);
num.erase(j, );
}
return res;
}
};
康托编码
Solution 3
class Solution {
public:
string getPermutation(int n, int k) {
string s = "", str;
int factorial = ;
for(int i = ; i < n; ++i){
factorial *= i;
}
--k;
for(int i = n; i > ; --i){
int index = k / factorial;
str += s[index];
s.erase(index, );
k %= factorial;
factorial /= i - ? i - : ;
}
return str;
}
};
【LeetCode】060. Permutation Sequence的更多相关文章
- 【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.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode】567. Permutation in String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...
- 【leetcode】Next Permutation
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
随机推荐
- vim对光标所在的数字进行增减
真是vim会在不经意间给你惊喜...... 现在发现把光标移到某数字的上方,c-a是加1, c-x是减1 当时真有点众里寻他千百度的感觉
- 还在用 kill -9 停机?这才是最优雅的姿势(转)
_ 最近瞥了一眼项目的重启脚本,发现运维一直在使用 kill-9<pid> 的方式重启 springboot embedded tomcat,其实大家几乎一致认为:kill-9<pi ...
- - symfony/icu v1.2.0 requires lib-icu >=4.4 -> the requested linked library icu has the wrong version installed or is missing from your system, ma
$ composer install Loading composer repositories with package information Installing dependencies (i ...
- java基础入门之九九乘法表
/* 自学java 九九乘法表 Power by Stuart Date: 2015.4.23 */public class Math { public static void main (Strin ...
- Javaweb--- EL表达式 JSTL标准标签库
一.EL表达式(expression language): 语法 ${...} jsp中page指令有一个属性叫isELIgnored, 用来标记此页面是否忽略EL表达式, 默认为false 举个例 ...
- R语言数据管理(四):数据导出
与read.*函数对应,导出函数为write.*函数. 比较常见的为write.csv和write.table. 一般格式: setwd("D:\\") write.table(y ...
- (转载)C#格式规范
前言 之前工作中整理的一篇编码规范. 代码注释 注释约定 只在需要的地方加注释,不要为显而易见的代码加注释使用 /// 生成的xml标签格式的文档注释 方法注释 所有的方法都应该以描述这段代码的功能的 ...
- 转 开启“大数据”时代--大数据挑战与NoSQL数据库技术 iteye
一直觉得“大数据”这个名词离我很近,却又很遥远.最近不管是微博上,还是各种技术博客.论坛,碎碎念大数据概念的不胜枚举. 在我的理解里,从概念理解上来讲,大数据的目的在于更好的数据分析,否则如此大数据的 ...
- lnmp 一键安装配置
l系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian/Deepin Server/Aliyun/Amazon/Mint Linux发行版 需要5GB以上硬盘 ...
- 各种IoC框架下实现AOP
.Net AOP(五) 各种IoC框架下实现AOP 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率 主要功能 日志记录,性 ...