描述:

  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. C-Free 您不能使用调试解决方案

    什么时候C-Free 当您调试 找不到gdb.exe解决方案 http://www.programarts.com/ C-Free 官方网站 下载Mingw或者其他编译器 版权声明:本文博主原创文章. ...

  2. ZOJ3784 String of Infinity 高大上的AC自动机 数据原来这么水啊!不算输入输出只有5-7行

    找给定s集合里面word全部是同一个字符的,这样的word有几个,如果数量<m就yes,否则就no.#include<iostream> #include<cstring> ...

  3. sql server 日期处理datediff

    语法: DATEDIFF(datepart,startdate,enddate) datepart 参数可以是下列的值: date-part : year | quarter | month | we ...

  4. Qt 圆角矩形+鼠标左键拖动窗口

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWind ...

  5. php7 不向后的兼容的变更

    php7 不向后的兼容的变更 在php7中,很多致命错误以及可恢复的致命错误,都被转换为异常来处理了. 这些异常继承自Error类,此类实现了Throwable接口(所有异常都实现了这个基础接口) 这 ...

  6. day8_python学习笔记_chapter11_函数

    1. 返回对象的数目   python实际返回的对象 0 -> None ; 1 -> object ; >1 -> tuple 2. 内部/内嵌函数:如果内部函数的定义包含了 ...

  7. Customizing Zend Studio Using the Welcome Page

    Customizing Zend Studio Using the Welcome Page Zend Studio enables you to add or remove plugins from ...

  8. 解决eclipse创建Maven项目后无法生成src/main/java资源文件夹的方法

    在项目上右键选择properties,然后点击java build path,在Librarys下,编辑JRE System Library,选择workspace default jre.

  9. Selenium自动化测试(java语言)

    Selenium介绍 Selenium 1.0 包含 core. IDE. RC. grid 四部分,  selenium 2.0 则是在两位大牛偶遇相互沟通决定把面向对象结构化( OOPP) 和便于 ...

  10. 【Howie玩docker】-使用mono编译c#程序

    根据前面的方法,在windows和Linux共享文件夹,然后就可以开发了! Start up an Ubuntu container $ docker run -it ubuntu bash Upda ...