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

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the kth permutation sequence.

如果有n个元素,第K个permutation是a1, a2, a3, .....   ..., an,那么a1是哪一个数字呢?



那么这里,我们把a1去掉,那么剩下的permutation为:a2, a3, .... .... an, 共计n-1个元素。 n-1个元素共同拥有(n-1)!组排列,那么这里就能够知道



设变量K1 = K-1

a1 = K1 / (n-1)!// 即a1是1~n中未使用过的第a1个元素,比如,刚開始时,若a1 = 1,则结果的第一个元素是2



同理,a2的值能够推导为

K2 = K1 % (n-1)!  //前面的a1*(n-1)!已经增加,所以要去掉

a2 = K2 / (n-2)!



。。。。。

K(n-1) = K(n-2) /2!

a(n-1) = K(n-1) / 1!



an = K(n-1)

class Solution {
public:
string getPermutation(int n, int k)
{
int data[10];//保存阶层的值
bool hashUse[10];
memset(hashUse,false,sizeof(bool)*10);
int i,j;
data[0] = data[1] = 1;
for(i = 2;i <= n;++i)data[i] = data[i-1] * i;
k --;
string res;
for(i = n - 1;i >= 0;--i)
{
int value = k / data[i];
for(j = 1;j <= n;++j)//查找第value大且未使用过的值
{
if(!hashUse[j])value--;
if(value < 0)break;
}
hashUse[j] = true;
res += j + '0';
k %= data[i];
}
return res;
}
};

leetcode 之 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

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

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

  9. 【leetcode】 Permutation Sequence

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

随机推荐

  1. CentOS 6.4 x86_64 安装GCC 4.7.3

    下载gcc-4.7.3.tar.gz, 解压缩 wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.7.3/gcc-4.7.3.tar.gz tar -xzvf gcc-4.7. ...

  2. WordPress的用户系统总结

    原文发表自我的个人主页,欢迎大家訪问~转载请保留本段,或注明原文链接:http://www.hainter.com/wordpress-user-module keyword:WordPress,用户 ...

  3. NetBeans 7.2 or 8.0 编辑文件时不显示文件路径。

    NetBeans 7.2 or 8.0 编辑文件时不显示文件路径. 仅仅实用鼠标停在标签上一下,才干够看到.非常不方便. 怎样解: http://plugins.netbeans.org/plugin ...

  4. uva 1374 快速幂计算

    #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #i ...

  5. Linux经常使用命令(十一) - more

    more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最主要的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...

  6. Codeforces Round #256 (Div. 2) 题解

    Problem A: A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. 【PHP SDK for OpenStack/Rackspace APIs】身份验证

    在你使用php-opencloud之前必须先取得云服务提供商的身份验证.这是整个过程中最简单也是最让人沮丧的部分. 说它最简单是因为你只需要两部分信息: 云服务提供商的身份验证端点 用来身份验证的证书 ...

  8. iOS 开发学习35 本地化

    增新语言 打开Project-Info-Localizations 点击Localization下的+ 新增语言 定义多语言文件 新增String Files 在Supporting Files上.新 ...

  9. Swift 可展开可收缩的表视图

    主要学习与运行效果 在本节的内容中,我们将通过一个具体实例的实现过程,详细讲解在表视图当中,如何创建一个可展开可收缩的表视图.为了让读者有着更为直观的印象,我们将通过模仿QQ好友列表来实现这个效果. ...

  10. 百度搜索结果页url参数详解

    在百度首页输入任意关键词搜索之后,我们跳转到搜索结果页面,在浏览器的网址栏我们可以看到很长的一串url地址.那么,你真的了解这一串url的含义吗? s?:搜索 百度搜索结果页使用了重定向,因此我们看到 ...