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. GreenDAO数据库版本升级

    GreenDAO是一款非要流行的android平台上的数据库框架,性能优秀,代码简洁. 初始化数据库模型代码的时候需要使用java项目生成代码,依赖的jar包已经上传到我的资源里了,下载地址如下:ht ...

  2. Python脚本:获取股票信息

    在水木上看到有人在问到想用python去获取股票的信息,sina finance上面的那些数据的是通过js控制的,会根据股票代码去获取实时信息然后根据用户友好的方式展示出来.首先,新浪的一个url让我 ...

  3. webdynpro的select_option示例

    需求,输入航线代码和航线编号区间,查询记录存在表中显示,并不是alv显示 1.使用组件WDR_SELECT_OPTIONS. 2.在组件控制器中加入以下组件 3.在视图属性中也添加该组件 4.创建节点 ...

  4. grub配置文件grub.conf详细说明

    说明:只供学习交流 default行,是指grub启动时默认菜单项.0表示第一项,如果是多系统可以修改此选项改变默认光标停留位置. timeout行,是指菜单到自动启动系统前的停留时间,单位时间为se ...

  5. VC++实现位图显示透明效果--实现原理

    我们在进行程序的界面设计时,常常希望将位图的关键部分,也既是图像的前景显示在界面上,而将位图的背景隐藏起来,将位图与界面很自然的融合在一起,本文介绍了透明位图的制作知识,并将透明位图在一个对话框中显示 ...

  6. 同事的Excel中的图片突然不能选择

    今天上午,同事突然说自己用的Excel不能编辑了,发来一看原来是其中做的图片不能编辑,鼠标放上去后显示个圆圈选不中. 在“视图”中调出“控件工具箱”工具栏,上面有一个三角板与直尺样子的按钮叫“设计模式 ...

  7. 嵌入式linux多进程编程

    嵌入式linux多进程编程 在主程序显示文本菜单.提供例如以下服务.要求每一个服务都通过生成子进程来提供. 服务包含:日历信息显示,日期信息显示,推断闰年服务,文件复制功能,数字排序功能.退出功能. ...

  8. SINGLETON(单例模式)---(孤独的人)

    很多时候,我们都很彷徨,因为,在身边的朋友,很少. package patterns.createable.singleton; /** * 孤独的人啊 * 我为你写了一个类 * 这个类,在我们的程序 ...

  9. 改动已有gpg密钥的用户标识及凝视

    /*********************************************************************  * Author  : Samson  * Date   ...

  10. 【源代码】基于Android和蓝牙的单片机温度採集系统

    如需转载请标明出处:http://blog.csdn.net/itas109 QQ技术交流群:129518033 STC89C52单片机通过HC-06蓝牙模块与Android手机通信实例- 基于And ...