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. Delphi中运行时改变panel的位置及大小(通过wm_SysCommand来实现)

    procedure TForm1.pnl1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Int ...

  2. Hadoop2.0/YARN深入浅出(Hadoop2.0、Spark、Storm和Tez)

    随着云计算.大数据迅速发展,亟需用hadoop解决大数据量高并发访问的瓶颈.谷歌.淘宝.百度.京东等底层都应用hadoop.越来越多的企 业急需引入hadoop技术人才.由于掌握Hadoop技术的开发 ...

  3. String 中的秘密

    Navigation:  数据类型相关 > Delphi 的字符及字符串 > [3] - String 中的秘密   //String 的指针地址及实际的内存地址 var str: str ...

  4. POJ 1466 最大独立集入门

    题意:n个学生,给你每个学生浪漫的学生学号(男女之间浪漫),问你找出一个最大的集合保证集合内的任意两个学生之间没有相互浪漫关系,输出最大集合的人数. 注意:这里的浪漫边是双向的,如果1对2浪漫, 那么 ...

  5. 请问,如何在windows系统下面同时使用中文和英文的cmd?_百度知道

    请问,如何在windows系统下面同时使用中文和英文的cmd?_百度知道 在批处理开始加一行chcp 437就是英文的cmdchcp 936就是中文的cmd

  6. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  7. 用 managedQuery() 时须要注意的一个陷阱

    Activity 里面提供了一个 managedQuery() 方法,依照 Android SDK 里面的说明,"the activity will manage its lifecycle ...

  8. linux公社的大了免费在线android资料

    2011年linux数据库的android在线分享 linux公社:开源公社             本文撰写:杨凯专属频道 2011年9月12日 21:39 <目录> Android 3 ...

  9. 积累的VC编程小技巧之列表框

    1.列表框中标题栏(Column)的添加 创建一个List Control,其ID为IDC_LIST,在其Styles属性项下的View项里选择Report.Align项里选择Top.Sort项里选择 ...

  10. Swift - 解析XML格式数据(分别使用GDataXML和DDXML)

    在做一些应用的时候经常需要用到XML解析,比如获取Web Service数据,读取RSS新闻或者博客数据源.下面演示了两个非常方便高效的XML库在Swift里的调用方法. 假设需要被解析的XML数据文 ...