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.

Note: Given n will be between 1 and 9 inclusive.

Backtracking Math

这道题最直观的的解法就是先求出全部的排列,然后再从结果中找到第k个值就可以。可是非常明显会超时。

假设不能先将全部的排列都求出来,那么这道题的目的就是让我们直接找到第k个排列了。

那么怎样找到第k个排列?直接要找到规律可能会比較困难,可是能够使用回溯和动态规划的一般方法,即使用用例来分析,从特殊到一般。看看通过这个特殊的用例能不能找到通用的方法,可是使用用例分析可能会因为用例选取的不全而导致遗漏一些情况,这道题做到最后就是用例选取的不全导致改了好久。

取n=3,k=5,那么输出应该是第5个排列”312”。

能够发现n=3时的全部排列中以1开头的排列有2个,以2开头的排列有2个,以3开头的排列有2个。

排列的个数取决于后面的数有多少种排列,这里后面有2个数,排列的个数是2!=2。

于是对于k=5能够这么分析

5/2=2;

5%2=1

即将[123]第0位的数字1和第2位的数字3交换,第0位就处理好了,如今数组变成[321],接着指针移到到第1位。然后将第1位到最后的元素排序。数组变成了[312],然后求[12]中的第1个数。

可是这样的求解方法会有一点问题,那就是本来5和6应该都是和第2位交换,可是因为6/2=3,结果变成了第0位和第3位交换,非常明显这是错误的,我们应该使用它在结果集中的下标来使用这个元素。对于k=5,实际上是第k-1=4个元素。对于4:

4/2=2;

4%2=0

它表示第0个元素要和第2个元素交换,这时第0个元素就处理好了,然后再在后面的2个元素构成的排列中查询第4%2=0个元素,当全部的元素都处理好了以后,这个数组中的元素就是我们要找的第k个排列了。

runtime:4ms

class Solution {
public:
string getPermutation(int n, int k) {
arr=new char[n];
for(int i=0;i<n;i++)
arr[i]=i+'1';
helper(0,n,k-1);
string str;
for(int i=0;i<n;i++)
str+=arr[i];
return str;
}
void helper(int pos,int num,int k)
{
if(pos==num-1)
return ;
int base=k/fac(num-pos-1);
int remain=k%fac(num-pos-1);
sort(arr+pos,arr+num);
swap(arr[pos],arr[pos+base]);
helper(pos+1,num,remain);
} int fac(int n)
{
int result=1;
for(int i=1;i<=n;i++)
result*=i;
return result;
}
private:
char *arr;
};

LeetCode60:Permutation Sequence的更多相关文章

  1. LeetCode31 Next Permutation and LeetCode60 Permutation Sequence

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  2. Leetcode60. Permutation Sequence第k个排列

    给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  3. [Swift]LeetCode60. 第k个排列 | Permutation Sequence

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

  4. 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 序列排序

    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 the p ...

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

  8. 60. Permutation Sequence

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

  9. [Leetcode] Permutation Sequence

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

随机推荐

  1. Spring思维课程导图——bean得实例化和bean的管理

  2. 关于GIT使用过程中遇到的问题

    npm构建,将所需要安装的依赖添加至package.json文件中,使用cnpm i进行安装 #拉去指定项目的默认分支: git pull http://username:password@gitla ...

  3. D3.js 力导向图(小气泡围绕中心气泡)

    html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3 ...

  4. Objective-C——Runtime理解

    动态语言 OC是一门不折不扣的动态语言,所以它的很多机制都是动态运行时决定的.这点和C语言不一样,C语言是静态绑定,也就是编译后所有的一切都已经决定了.这一点和C语言的函数指针有些类似,很多时候函数指 ...

  5. [ Luogu Contest 10364 ] TG

    \(\\\) \(\#A\) 小凯的数字 给出两个整数\(L,R\),从\(L\)到\(R\)按顺序写下来,求生成整数对\(9\)取模后的答案. 例如\(L=8,R=12\),生成的数字是\(8910 ...

  6. Canvas清空

    当canvs与bitmap绑定时,canvas上绘制会导致bitmap改变内容,而且内容时叠加的.这时候需要清空bitmap上的内容,可以用以下做法. Paint paint = new Paint( ...

  7. html5——颜色

    CSS2 1.opacity,可以设置透明度,但是父盒子设置了透明度会影响子盒子 CC3 1.transparent属性,但是不可改变透明值 2.rgba():r--red g--green b--b ...

  8. [Windows Server 2012] Apache+PHP安全设置

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:Win20 ...

  9. bootstrap table 生成的表格里动态添加HTML元素按钮,JS中添加点击事件,点击没反应---解决办法

    bootstraptable中onExpandRow属性---js  方法添加的 html代码,然后给这代码里面的 元素 添加 事件,却获取不该元素.(称之为未来元素),由于是未来的 所以现在没有这个 ...

  10. 字符串问题:去掉字符串中连续出现 k 个 0 的子串

    [题目] 给定一个字符串 str 和 一个整数 k, 如果 str 中正好有连续 k 个 ‘0’ 字符出现时,把 k 个连续的 ‘0’ 字符去除,返回处理后的字符串. [举例] str="A ...