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. html网页中 点击按钮页面跳转

    在html页面中 实现点击按钮页面跳转.语句如下: <input type="button" value="跳转" onClick="windo ...

  2. perl post 请求带参数

    my $url='https://wenjinbao.winfae.com/business/dispatch_post.do?action=submitAdminLogin';    my $res ...

  3. 基于visual Studio2013解决C语言竞赛题之1052求根

       题目 解决代码及点评 /* 功能:用简单迭代法解方程 e^x - x - 2 = 0 它有两个根(如图),其迭代公式为: 1) x[n+1]= e^x*n-2 (初值X<0时) ...

  4. VB与报表的交互

    还接着上次说.在上个博客中已经说到建立好表的步骤了,接下来就是怎么使表与vb连接. 先看一下代码. Option Explicit Dim WithEvents Report As grproLibC ...

  5. 486E - LIS of Sequence(LIS)

    题意:给一个长度为n的序列.问每一个数关于序列的LIS(longest increasing subsequence)是什么角色. 这里分了三种: 1.此数没有出如今随意一条LIS中 2.此数出如今至 ...

  6. JDBC操作数据库的学习(2)

    在上一篇博客<JDBC操作数据库的学习(1)>中通过对例1,我们已经学习了一个Java应用如何在程序中通过JDBC操作数据库的步骤流程,当然我们也说过这样的例子是无法在实际开发中使用的,本 ...

  7. Android开源框架AsyncHttpClient (android-async-http)使用

    android-async-http 开源框架可以使我们轻松地获取网络数据或者向服务器发送数据,最关键的是,它是异步框架,在底层使用线程池处理并发请求,效率很高,使用又特别简单. 以往我们在安卓上做项 ...

  8. ViEmu For VS2010 3.0 解除30天限制的方法

    一.概述 首先,ViEmu试用版在安装时会记录安装的时间,用于判断是否已经过了限制的时间,这个时间记录在注册表中 以本人的机器(WIN7X64)为例,它记录在 HKEY_CLASSES_ROOT\Wo ...

  9. ABP启动配置

    ABP启动配置   返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行 ...

  10. Android面向HTTP协议发送get请求

    /** * 採用get请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ pub ...