【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)法. ...
随机推荐
- Unity3d 摇杆奖励
单个单元: publicclass RockerSingle : MonoBehaviour { // 枚举.类别 RockerType rockerType; //是否有效,最上面的为无效,即为f ...
- Computer Transformation(简单数学题+大数)
H - Computer Transformation Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- F - Monkey Banana Problem
F - Monkey Banana Problem Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & ...
- EasyNVR无插件直播服务器软件接口调用返回“Unauthorized”最简单的处理方式
背景需求 对于EasyNVR的受众群体十分的广泛,不仅仅有将EasyNVR作为视频直播平台直接使用的,更多的是使用EasyNVR的对应功能集成到自身系统.对于前者,只需要将软件的使用功能搞清楚即可,对 ...
- Xshell调节字体大小和样式
有时候没有看着字体太小的,好难受, 调节字体大小: ALT+P快捷键打开
- 5.设计模式----prototype原型模式
原型模式:做到是原型,那肯定是自己本身才是原型,原型模式属于对象的创建模式. 关于原型模式的实现方式分2种: (1)简单形式.(2)登记形式,这两种表现形式仅仅是原型模式的不同实现. package ...
- spring mvc注解和spring boot注解
1 spring mvc和spring boot之间的关系 spring boot包含spring mvc.所以,spring mvc的注解在spring boot总都是可以用的吗? spring b ...
- TFS中工作项的定制- 字段功能定义
参考,翻译此页面All FIELD XML Elements Reference(http://msdn.microsoft.com/en-us/library/ms194953.aspx) 对于每一 ...
- Django 模型系统(model)&ORM--进阶
QuerySet 可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. >>> Entry.objects.all()[ ...
- js面对对象编程
说到js,非常大一部分人会说我非常熟悉,在日常的web开发中经经常使用,那么你的js代码是符合面对对象思路的吗?那你会问我面向过程的js代码有什么不好吗?我的感受是面对对象的js编码更加简洁,降低了混 ...