描述:

  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):

    1.   "123"
    2.   "132"
    3.   "213"
    4.   "231"
    5.   "312"
    6.   "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(康托展开)的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  6. [LeetCode]60. Permutation Sequence求全排列第k个

    /* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...

  7. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  8. 【一天一道LeetCode】#60. Permutation Sequence.

    一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  9. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 六行代码获取本地IP

    uses IdIPWatch; function GetNativeIP: String; var IdIPWatch: TIdIPWatch; begin IdIPWatch := TIdIPWat ...

  2. AngularJs(三) deployd 服务的使用

    使用服务建立数据 在AngularJS(二)中,我搭建好了deployd服务,现在启动服务,创建正在的数据(开始是使用模拟数据),使用cmd命令 一.开启Mongodb数据. 贴图: 二:测试是否正常 ...

  3. Java面试题之weblogic相关问题

    WebLogic是美国Oracle公司出品的一个application server确切的说是一个基于JAVAEE架构的中间件,BEA WebLogic是用于开发.集成.部署和管理大型分布式Web应用 ...

  4. C#中的枚举类型

    浅谈C#中的枚举  转自http://www.cnblogs.com/liyingyi/archive/2005/12/28/306772.aspx   枚举类型是一种的值类型,它用于声明一组命名的常 ...

  5. C#程序之Main()方法

    一.Main()方法的简介 1.一般情况下,一个C#可执行程序只有一个应用程序对象(也就是就程序入口),但是在某些情况,可能会有多个应用程序对象(程序入口),如单元测试中,这个时候我们就需要通过命令行 ...

  6. JavaScript的日期处理

    很久前写的代码了,偶尔看到贴出来做个备忘,写得有点乱,懒得整理了. // 根据初始日期和滚动周期及滚动次数来计算终止日期,日期滚动周期,// 可以是每天(DAY).每周(WEEK).每月(MONTH) ...

  7. api(一) 创建窗口 (转)

    所有的Windows SDK编程都有一个类似的框架,本文就说说这个框架,Windows程序设计的框架分为“三部曲”: 一.注册窗口类 注册窗口类的API函数是RegisterClass或者Regist ...

  8. hdu3516

    题目大意:这个....翻译起来还真是不好说,各位四六没过的ACMer正好去原网页看看题意,过了的好孩子还是去看看原网页看看锻炼一下吧.(当然我做这道题目的时候,教练已经摆明说要用四边形不等式,所以还是 ...

  9. HTML DOM 属性记录

    将HTML DOM中几个容易常用的属性做下记录,需要的朋友可以参考下.     nodeName.nodeValue 以及 nodeType 包含有关于节点的信息. nodeName 属性含有某个节点 ...

  10. win8VPN

    上一章已经讲过Windows2008RT搭建VPN服务器搭建过程,接下来说一下win8的VPN登录 这里是win2008的VPN连接过程 先说win8的VPN登录过程.同样也很简单步骤和2008的差不 ...