一天一道LeetCode系列

(一)题目

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.

(二)解题

第一种解法:

参考:【一天一道LeetCode】#31. Next Permutation这篇博客,很奇怪的是为什么n=8,k=8590的时候会超时,暂时还没有想出来原因,欢迎大家留言讨论。

/*
利用STL的next_permutation每次求出下一个排列数
*/
class Solution {
public:
    string getPermutation(int n, int k) {
        string seq;
        string ret;
        for(int i = 0 ; i < n ; i++)
        {
            seq+=('1' + i);
        }
        do
        {
            k--;
            if(k==0) {
                ret = seq;
                break;
            }
        }while(next_permutation(seq.begin(),seq.end()));
        return ret;
    }
};

第二种解法:

利用排列数的规律来求解。

我们以题目给出的例子为例来讲解规律。 n=3时,由1,2,3三个数组成{a1,a2,a3}

1:”123”

2:”132”

3 : “213”

4 :”231”

5 : “312”

6 : “321”

首先看第一个数a1(候选数字temp = “123”),k为1,2时,a1=1,k为3,4时,a1=2,k为5,6时,a1=3,从中可以看出a1=temp[(k-1)/(n-1)!]。

确定了第一个数后我们来看第二个数a2,以1,2这一组来看,排除了1,候选数字temp = “23”,这个时候k只能为1和2,所以在上一步中k%=(n-1)!,这个时候a2=temp[(k-1)/(n-2)!]

最后a3只能为剩下的一个数字了。

ok,整理一下思路,我们需要一个候选字符串temp,一个(n-1)!的数组f[10] = {1,1,2,6,24,120,720,5040,5040*8}(n为1~9的数字),k初始值为k-1

在每一步中,a1=temp[k/f[n-1]],k%=f[n-1]。一直到n为0。

具体思路见代码:

class Solution {
public:
    string getPermutation(int n, int k) {
        string temp = "123456789";
        int f[] = {1,1,2,6,24,120,720,5040,5040*8};
        string ret;
        int i = n;
        k--;//k的初始值
        while(i)
        {
            int m = k/f[i-1];
            k %=f[i-1];
            ret+=temp[m];
            temp.erase(temp.begin()+m);//擦除掉已经选掉的值
            i--;
        }
        return ret;
    }
};

【一天一道LeetCode】#60. Permutation Sequence.的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. [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 ...

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

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

  6. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  7. [LeetCode]60. Permutation Sequence求全排列第k个

    /* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...

  8. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  9. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. ACM Ignatius and the Princess II

    Problem Description Now our hero finds the door to the BEelzebub feng5166. He opens the door and fin ...

  2. proc文件系统探索 之 根目录下的文件[二]

    包括对proc根目录下stat,uptime,swaps三个文件的解析. /proc/stat 文件包含了系统启动后的一些系统统计信息. Cat /proc/stat: cpu 77781 1077 ...

  3. Kafka系列之-自定义Producer

    前面已经讲到了,在Kafka中,Message是由Producer产生的,Producer产生的Message会发送到Topic的指定Partition中.Producer可以有多种形式,也可以由用户 ...

  4. Servlet常用操作(基础)

    ----------------------------------------------------------------------------------------------[版权申明: ...

  5. Racket 模拟SICP的流(延时计算)

    默认的Racket是要对函数参数进行求值的, 例如(f 1 (+ 1 2))里面,(+ 1 2)要先求值为3,变为(f 1 3)再进行下一步操作.因此, Racket若按照SICP使用define关键 ...

  6. Android EditText在ScrollView中被输入法遮挡

    千言万语不如一张图来的实在,问题如下GIF图所示[输入框被输入法挡住了]: 为了不让底部的按钮随着输入法一起起来,我把windowSoftInputMode设置为adjustPan. <acti ...

  7. Web Service进阶(三)HTTP-GET, HTTP-POST and SOAP的比较

    XML Web Service支持三种协议来与用户交流数据.这三种协议分别是: 1.SOAP:Simple Object Access Protocol 2.HTTP-GET 3.HTTP-POST ...

  8. HDFS:NameNode、DataNode、SecondaryNameNode

    可以一句话描述 HDFS:把客户端的大文件存放在很多节点的数据块中. HDFS设计原则: 1,文件以块(block)方式存储: 2,通过副本机制提高可靠度和读取吞吐量: 3,每个区块至少分到三台Dat ...

  9. iOS应用程序工程文件以及启动流程

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51351188 本文出自:[openXu的博客] iOS程序启动流程 完整启动流程 UIApp ...

  10. 如何优雅地退出python程序

    如何优雅地退出python程序 一个单模的python程序,启动之后要能够优雅地关闭.即当用户按Ctrl+C或者kill pid的时候,程序都能从容关闭.实现起来非常简单. is_running = ...