一天一道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. Microsoft Dynamics 365 Developer Toolkit下载地址

    下载,支持Visual Studio 2012, 2013, 2015

  2. pandas小记:pandas高级功能

    http://blog.csdn.net/pipisorry/article/details/53486777 pandas高级功能:面板数据.字符串方法.分类.可视化. 面板数据 {pandas数据 ...

  3. linux下内存大小、起始地址的解析与修改

    在实际的工作中,由于产品型号的不同,经常需要调整linux所管理的内存的大小,而内核在启动阶段,会两次去解析从uboot传递过来的关于内存的信息,具体如下: 一.解析从uboot传递过来的tag(在p ...

  4. Docker学习笔记1:CentOS7 下安装Docker

    本文内容摘自官网:https://docs.docker.com/engine/installation/linux/centos/#/create-a-docker-group 注:本文是介绍Lin ...

  5. 高端技巧:如何使用#define定义变量

    Introduction 想在源文件中定义一个跟行号有关的变量,每次都手动输入实在是太慢了,本文介绍如何使用宏定义来定义与行号有关的变量. 例如:我们想在源代码的第10行定义A_10这样的一个整形变量 ...

  6. ViewPager实现首次进入软件时左右滑屏的软件展示效果

    效果如图: 图片资源不再提供,大家可以自己下载,能实现效果即可,看代码: 首先是展示界面的layout: view.xml 注意,采用的是帧布局,页面切换时的小圆点是在各张图片之上的 <?xml ...

  7. The packages can be overrided by Java Endorsed Standards

     Endorsed Standards APIs The Endorsed Standards for Java SE constitute all classes and interfaces ...

  8. 全文检索概念,Lucene大致结构

    1.1 常见的全文检索 1) 在window系统中,可以指定磁盘中的某一个位置来搜索你想要得到的东西. 2) 在myeclipse中,点击Help->Help Contents,可以利用搜索功能 ...

  9. Hadoop MapReduce工作原理

    在学习Hadoop,慢慢的从使用到原理,逐层的深入吧 第一部分:MapReduce工作原理   MapReduce 角色 •Client :作业提交发起者. •JobTracker: 初始化作业,分配 ...

  10. VS2010 express中改变VC Default include/lib/… 目录

    转自: Liz's Blog http://www.cnblogs.com/lizmy/archive/2012/01/10/2318258.html 2010中是以工程为单位,更改VC++ dire ...