leetcode 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.
思路:
参见我的博文:全排列的编码与解码:康托展开
代码:
class Solution {
public:
string getPermutation(int n, int k) {
vector<int> temp;
vector<int> fac;
vector<int> nums;
int next_k,now_seq;
string result;
if( n== ){
result="";
return result;
}
//generate nums
for( int i=;i<n;i++ )
nums.push_back(i+);
//calculate factorial times,fac[0]=(n-1)!,fac[1]=(n-2)!
for( int i=n-;i>=;i-- ){
if( i==n- )
fac.push_back();
else
fac.insert(fac.begin(),(n-i-)*(*fac.begin()));
}
//calculate every place from high
next_k=k;
for( int i=;i<n-;i++ ){
now_seq=(next_k-)/fac[i]+;
temp.push_back(nums[now_seq-]);
nums.erase(nums.begin()+now_seq-);
next_k=(next_k-)%fac[i]+;
}
temp.push_back(nums[]);
//into string
for( int i=;i<n;i++ ){
result.append(,''+temp[i]);
}
return result;
}
};
leetcode 60. Permutation Sequence(康托展开)的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [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 the p ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- [LeetCode]60. Permutation Sequence求全排列第k个
/* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...
- 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.
一天一道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 ...
随机推荐
- 用PS绿化版出现“请卸载并重新安装该产品”的解决方法
下载了一个CS6版本的绿化版PS,解压后发现用不了,因为是不用安装的,所以这个提示明显是没用的. 我把64位破解文件 amtlib.dll和32位破解文件 amtlib.dll都放进去试了一下,结果行 ...
- 文件和文件夹权限-Win7公共盘中出现大量临时文件
公司中有一个文件服务器,给不同部门和员工设置了不同的权限,最近有员工(没有修改权限,有读取及执行,读取,写入)反映在公共盘上修改文件的时候会产生大量的临时文件,添加上修改权限之后就可以了,然后被同事问 ...
- 创建ORACLE 查询用户
[apptest@vis appl]$ su -oravis [oravis@vis 11.1.0]$ sqlplus / as sysdba SQL> create user erpquery ...
- UIlabel添加背景图片
做UI的时候我们可能想给某个Label添加一个背景图片,但查看的时候会发现好像只有设置背景颜色的方法,不过我们也可以通过这种方式来解决: UIColor *color = [UIColor color ...
- Java多线程-新特征-锁
Java中读写锁有个接口java.util.concurrent.locks.ReadWriteLock,也有具体的实现ReentrantReadWriteLock,详细的API可以查看JavaAPI ...
- 解决ie6里png图片透明变白色bug
加入这段js就行了. function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6. { var a ...
- nginx 目录密码保护的设置方法
在 nginx.conf 文件中对应的 server 段中 添加 location ^~ /test/ { auth_basic TEST-Login; auth_basic_user_file /r ...
- Red Hat Enterprise Linux Release Dates
Red Hat Enterprise Linux Release Dates UpdatedMay 10 2016 at 10:57 PM - English The tables below lis ...
- Android切换页面效果的实现一:WebView+ViewFlipper
前言: 这两周在帮学校做一个新生入学用的“新里程”的项目,要做到页面切换阅读的效果,自己百度了下,找到普遍是使用WebView+ViewFlipper的实现方法,但这种方法不能满足我的要求,因为它很难 ...
- Day1_PHP快速入门
本人知识背景:行业软件C/C++开发两年经验,了解PHP, 所以学习日志偏向记录PHP相对于C的特性 测试环境:EasyPHP13.1 Day 1 学习时间:3小时 1. HTML触发PHP HTML ...