题目

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):

"123"
"132"
"213"
"231"
"312"
"321"

Given n and k, return the kth permutation sequence.

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

解答

这里用到按序排列的一个性质,就是长度为n的数列的第k个排列的最高位,等于这个数列中的第ceil(k / (n - 1)!)位.

下面是AC的代码:

class Solution {
public:
    long long fac(int n){
        int ret = 1;
        while(n > 1){
            ret *= n;
            n--;
        }
        return ret;
    }
    string getPermutation(int n, int k) {
        long long total = fac(n);
        vector<int> left;
        string ans;
        int length, temp, digit;

        for(int i = 1; i <= n; i++){
            left.push_back(i);
        }
        for(int i = n; i > 0; i--){
            total /= i;
            temp = k / total;
            if(k != temp * total){
                digit = left[temp];
                left.erase(left.begin() + temp);
            }
            else{
                digit = left[temp - 1];
                left.erase(left.begin() + temp - 1);
            }
            ans = ans + to_string(digit);
            if(k != temp * total){
                k -= temp * total;
            }
            else{
                k -= (temp - 1) * total;
            }
        }
        return ans;
    }
};

112

LeetCode OJ 60. Permutation Sequence的更多相关文章

  1. 【一天一道LeetCode】#60. Permutation Sequence.

    一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

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

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

  3. 【LeetCode】60. Permutation Sequence

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

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

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

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

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

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

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

随机推荐

  1. 汽车车牌JS正则表达式验证(含新能源车牌)

    /** * 第一:普通汽车 * 车牌号格式:汉字 + A-Z + 5位A-Z或0-9( 车牌号不存在字母I和O防止和1.0混淆) * (只包括了普通车牌号,教练车,警等车牌号 .部分部队车,新能源不包 ...

  2. cas server

    Tomcat: V8.5.x Java: 1.8 x64 MySQL: 5.5.x OS: Win10 x64 I. war 0. clone git clone https://github.com ...

  3. Android Studio连接不到MuMu模拟器;

    网易推出的mumu模拟器还挺好用的,主要是流畅占内存小: 但是安装mumu模拟器后,as连接不到mumu模拟器: 好了,教程来了:两步走: 先把模拟器运行起来! 第一步:打开Terminal,输入:  ...

  4. Windows Server 2012 NAT端口转发

     

  5. 解决从客户端(Content="<div><p ><p>12312...")中检测到有潜在危险的Request.Form 值。

    [HttpPost] [ValidateInput(false)]//解决从客户端(Content="<div><p ><p>12312..." ...

  6. 零基础学习python_字典(25-26课)

    今天学到后面的知识,突然发现之前学习到的字典列表啥的都有点忘了,打算补一下之前学到的字典,到时候你看的时候,字符串.列表.字典.元祖这几个没啥顺序,刚开始学的时候了解下方法,当然你可以死记硬背下,后面 ...

  7. 重识linux-守护进程,系统服务,daemons

    重识linux-守护进程,系统服务,daemons 1分类 分为 单独的守护进程 和超级守护进程 2命名 服务的名称被创建之后,被挂上linux使用,通常在服务的名称之后会加上一个d,例如at和cro ...

  8. python入门-python处理csv文件格式相关

    python入门-python处理csv文件格式相关 处理 下载的csv格式文件 直接上代码和效果图 import csv from datetime import datetime from mat ...

  9. SD-WAN供应商列表

    SD-WAN的一个重要思想是,可以使用任何类型的多个物理WAN链路来承载流量,而无需网络工程师进行大量工程设计.相反,SD-WAN解决方案在物理基础设施之上运行覆盖(隧道),抽象出实际链接. SD-W ...

  10. hive orc update

    hive-site.xml --><configuration> <!-- WARNING!!! This file is auto generated for documen ...