Permutation Sequence [LeetCode]
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.
Solutions: Caculates the index of the first number by idx = (k -1) / (n-1)! , and the new k and new n, then goes to next round.
string getPermutation(int n, int k) {
vector<int> nums;
vector<int> factors(,);
for(int i = ; i <= n; i++){
nums.push_back(i);
factors.push_back(factors[i - ] * i);
}
string ret;
while(k > && nums.size() > ) {
int idx = (k -) / factors[n -];
ret.push_back(nums[idx] + );
nums.erase(nums.begin() + idx);
k = k - idx * factors[n - ];
n --;
}
return ret;
}
Permutation Sequence [LeetCode]的更多相关文章
- Permutation Sequence leetcode java
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence
一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- [leetcode]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
随机推荐
- echo输出到stderr
echo "Your error message here" >&2 This is a normal echo (which goes to stdout), ho ...
- HQL语句大全(转载)
Hibernate配备了一种非常强大的查询语言,这种语言看上去很像SQL.但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可以理解如继承.多态 和关联之类的概念. ...
- [Lonlife1031]Bob and Alice are eating food(递推,矩阵快速幂)
题目链接:http://www.ifrog.cc/acm/problem/1031 题意:6个水果中挑出n个,使得其中2个水果个数必须是偶数,问有多少种选择方法. 设中0代表偶数,1代表奇数.分别代表 ...
- LINUX DIFF命令详解
刚才在和公司做离线IP对比,最后手工了,感觉还是比较麻烦的,遇到数据很大的时候不能手工进行了 本想用linux下的DIFF来进行对比,发现结果很乱.时间很紧最后还是手工了. 现在忙完要认认真真学习一下 ...
- mount命令
注:硬件设备由linux系统自动识别,但必须成功挂载后才能使用 mount #查询已挂载 mount -a #依据配置文件/etc/fstab的内容自动挂载 挂载命令格式: mount [-t 文件 ...
- Monocular Vision
Monocular Vision: a condition in which one eye is blind, seeing with only one eye Binocular Vision: ...
- Webbrowser控件判断网页加载完毕的简单方法 (转)
摘自:http://blog.csdn.net/cometnet/article/details/5261192 一般情况下,当ReadyState属性变成READYSTATE_COMPLETE时,W ...
- CSS3 transition/transform
Transition 1.简写属性transition,可以包括四个属性,这四个属性的顺序按照下面介绍的顺序书写,即transition:property duration timing-functi ...
- gitHub-初识
如何生成github所使用的ssh key https://help.github.com/articles/generating-ssh-keys/ 多个github帐号的SSH key切换 htt ...
- HDU5730 FFT+CDQ分治
题意:dp[n] = ∑ ( dp[n-i]*a[i] )+a[n], ( 1 <= i < n) cdq分治. 计算出dp[l ~ mid]后,dp[l ~ mid]与a[1 ~ r-l ...