题目

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 vue-resource 请求数据

    main.js import Vue from 'vue'; import App from './App.vue'; /*使用vue-resource请求数据的步骤 1.需要安装vue-resour ...

  2. 第2章 GNS3和PacketTracer网络模拟器(1)_GNS3概述

    1. 安装和配置GNS3 1.1 GNS3概述 (1)GNS3是一款具有图形化界面,可运行在多平台(包括Windows.Linux.Mac OS等)上面的网络虚拟软件. (2)可以在虚拟环境中运行Ci ...

  3. C#根据进程名称获取进程的句柄?

    C#根据进程名称获取进程的句柄或C#如何获取其他进程的句柄? 有时候标题名是动态变化的,所以不使用FindWindow方法! [StructLayout(LayoutKind.Sequential)] ...

  4. C# JToken类的使用,实现解析动态json数据、遍历、查找

    在原来解析json数据是,一般都是用反序列化来实现json数据的解读,这需要首先知道json数据的结构并且建立相应的类才能反序列化,一旦遇到动态的json数据,这种方法就不使用. 为了解决动态解析js ...

  5. [ZJOI2015]诸神眷顾的幻想乡(广义后缀自动机)

    /* 题目中的神仙性质真的是令人愉悦 因为我一眼看成了每个点的度数不超过二十, 心想这他喵的和字符串什么关系 统计树上不同子串个数, 按道理直接dfs n次把所有的串插到后缀自动机里就行了 但是我们发 ...

  6. linux学习思维导图(转)

    转自:https://blog.csdn.net/m1585761297/article/details/80017111 先附上一张学习路径的导图 导图一 导图二(一套) 1.Linux目录结构 2 ...

  7. VS2015 无法启动 IIS服务器

    打开VS2012解决方案资源管理器 -> 点选 Web 项目选择 -> 属性 -> Web ->创建虚拟目录. 再次运行Web项目,成功.

  8. chattr和lsattr命令的使用(对于root用户也无法修改删除的操作问题)

    1.chattr命令的用法:chattr [+-=] [ASacdistu] File/Directory 参数说明: +-= :分别为 [+ 增加] [- 减少] [= 设定] 属性的意思 A :当 ...

  9. ERROR: iterator not incrementable || iterator not decrementable

    这个错误提示:迭代器不可以增加 exmaple: vector<int> tVecInt; vector<int>::reverse_iterator iterInt = tV ...

  10. jquery IE中加载xml

    $.ajax({ url: 'xml/myXML.xml', dataType: ($.browser.msie) ? "text" : "xml", time ...