求第k个排列。

刚开始按照一个排列一个排列的求,超时。

于是演算了一下,发下有数学规律,其实就是康托解码。

康托展开:全排列到一个自然数的双射

X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+a2*1!+a1*0!

ai为整数,并且0<=ai<i(1<=i<=n)

适用范围:没有重复元素的全排列

全排列的解码

如何找出第16个(按字典序的){1,2,3,4,5}的全排列?

1. 首先用16-1得到15

2. 用15去除4! 得到0余15

3. 用15去除3! 得到2余3

4. 用3去除2! 得到1余1

5. 用1去除1! 得到1余0

有0个数比它小的数是1,所以第一位是1

有2个数比它小的数是3,但1已经在之前出现过了所以是4

有1个数比它小的数是2,但1已经在之前出现过了所以是3

有1个数比它小的数是2,但1,3,4都出现过了所以是5

最后一个数只能是2

所以排列为1 4 3 5 2

class Solution{
public:
string getPermutation(int n, int k)
{
//get fractial
vector<int> fractial;
fractial.push_back();
for(int i = ;i<n;i++)
{
fractial.push_back(fractial[i-]*(i+));
}
//to mark if this digit selected ,true means can be selected, false means already selected.
vector<bool> allnum;
for(int i = ; i <=n; i++)
allnum.push_back(true); int ChuShu = k - , YuShu = ;
string ans;
int weishu = ; while(weishu<n)
{
int _num_i;
int place;
if(weishu == n-) // the last digit
_num_i = select(allnum,);
else
{
YuShu = ChuShu % fractial[n--weishu];
place = ChuShu / fractial[n--weishu]; _num_i = select(allnum,place + );
}
ChuShu = YuShu;
weishu ++;
char ch = '' - + _num_i;
ans += ch;
}
return ans;
}
int select(vector<bool> &allnum,int place)
{
int i = ; while(place)
{
if(allnum[i] == true)
{
place--;
if(place == )
break;
}
i++;
}
allnum[i] = false;
return i;
}
}; int main()
{
class Solution myS;
cout<<myS.getPermutation(,);
return ;
}

LeetCode OJ--Permutation Sequence *的更多相关文章

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

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

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

  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】 Permutation Sequence (middle)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  6. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  7. leetcode 之 Permutation Sequence

    Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  8. 【Leetcode】Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

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

  10. 【leetcode】 Permutation Sequence

    问题: 对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小開始直接到k, ...

随机推荐

  1. Python基础:字符串(string)

    字符串的常用操作 字符串与数组一样,支持索引操作.切片与遍历 索引.切片操作: name = 'jason' name[0] 'j' name[1:3] 'as' 遍历: for char in na ...

  2. matplotlib 设置图形大小时 figsize 与 dpi 的关系

    matplotlib 中设置图形大小的语句如下: fig = plt.figure(figsize=(a, b), dpi=dpi) 其中: figsize 设置图形的大小,a 为图形的宽, b 为图 ...

  3. 原来针对新唐mcu,keil有免费许可

    MDK for Nuvoton Cortex-M0/M23:The MDK for Nuvoton Cortex-M0/M23 is a license paid by Nuvoton. It is  ...

  4. Linux中同步与异步、阻塞与非阻塞概念以及五种IO模型

    1.概念剖析 相信很多从事linux后台开发工作的都接触过同步&异步.阻塞&非阻塞这样的概念,也相信都曾经产生过误解,比如认为同步就是阻塞.异步就是非阻塞,下面我们先剖析下这几个概念分 ...

  5. 什么是Maven?

    Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 发文时,绝大多数开发人员都把 Ant 当作 Java 编程项目的标准构建工具.遗憾的是, ...

  6. JSON Undefined 问题

    在IE6和IE7浏览器下或在IE8-IE10浏览器文档模式为IE7及以下时,控制台会报错:JSON is undefined. 这种错误在IE6和IE7浏览器下出现很正常,因为JSON在IE8+浏览器 ...

  7. 精通CSS高级Web标准解决方案(2-1 可视化格式模型之框模型)

    浮动.定位.框模型这些控制在页面上安排和显示元素的方式,形成CSS布局. 盒子模型 页面上的每个元素都被看成一个矩形框. 盒子模型有两种,分别是 IE 盒子模型和标准 W3C 盒子模型.他们对盒子模型 ...

  8. 聊聊、Nginx GDB与MAIN

    上一篇文章主要介绍了 Nginx 在 Window 和 Linux 平台上的安装.本章节主要介绍 Nginx 源码学习方法和源码结构,以及 Nginx 启动时 main 方法的位置,参数信息.后面的章 ...

  9. Stringsobits(模拟)

    描述 Consider an ordered set S of strings of N (1 <= N <= 31) bits. Bits, of course, are either ...

  10. Thinkphp5.1手册太简单,有的功能用起来不确定结果是否和预料的一样,顾整理记录

    //模板{if false} 1 {else/} //====>可以使用 效果同 {else /} 2 {/if} {if condition="(1 eq 1) and false& ...