题目

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. vue 父组件主动获取子组件的数据和方法 子组件主动获取父组件的数据和方法

    Header.vue <template> <div> <h2>我是头部组件</h2> <button @click="getParen ...

  2. Abp.Linq.Extensions扩展(1)--static class QueryableExtensions

    // 摘要:        //     Used for paging with an Abp.Application.Services.Dto.IPagedResultRequest object ...

  3. CentOS安装Git,并上传、下载

    转自:https://blog.csdn.net/u014066037/article/details/70224780 Git 一.安装Git $ yum install git 二. 生成SSH密 ...

  4. 将.ipynb文件导入到另外的文件中

    在定义函数或者类的.ipyter文件的末尾加上 try: !jupyter nbconvert --to python ppp.ipynb #ppp是文件的名称 except: pass 在需要导入到 ...

  5. linux下的进程信息管理

  6. 第8章 传输层(1)_TCP/UDP协议的应用场景

    1. 传输层的两个协议 1.1 TCP和UDP协议的应用场景 (1)TCP协议:如果要传输的内容比较多,需要将发送的内容分成多个数据包发送.这就要求在传输层用TCP协议,在发送方和接收方建立连接,实现 ...

  7. /storage/xx-xx/, /sdcard, /mnt/sdcard 三者的区别

    本文针对Android 7.1 /sdcard是/mnt/sdcard的符号链,指向/storage/self/primary, /mnt/sdcard,也是符号链,指向/storage/self/p ...

  8. (转)WebApi返回Json格式字符串

    原文地址:https://www.cnblogs.com/elvinle/p/6252065.html WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉 ...

  9. HBase原理和架构

    HBase是什么 HBase在生态体系中的位置 HBase vs HDFS HBase表的特点 HBase是真正的分布式存储,存储级别达到TB级别,而才传统数据库就不是真正的分布式了,传统数据库在底层 ...

  10. KVM总结-KVM性能优化之内存优化

    我们说完CPU方面的优化(http://blog.csdn.net/dylloveyou/article/details/71169463),接着继续第二块内容,也就是内存方面的优化.内存方面有以下四 ...