题目

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. 深入理解Apache Flink

    Apache Flink(下简称Flink)项目是大数据处理领域最近冉冉升起的一颗新星,其不同于其他大数据项目的诸多特性吸引了越来越多人的关注.本文将深入分析Flink的一些关键技术与特性,希望能够帮 ...

  2. hive计算周一的日期

    ) FreeMarker --',-7)?date('yyyy-MM-dd'),'week')?string('yyyy-MM-dd')}'

  3. JVM总结-反射

    反射是 Java 语言中一个相当重要的特性,它允许正在运行的 Java 程序观测,甚至是修改程序的动态行为. 举例来说,我们可以通过 Class 对象枚举该类中的所有方法,我们还可以通过 Method ...

  4. Unity3D的断点调试功能

    断点调试功能可谓是程序员必备的功能了.Unity3D支持编写js和c#脚本,但很多人可能不知道,其实Unity3D也能对程序进行断点调 试的.不过这个断点调试功能只限于使用Unity3D自带的Mono ...

  5. 1950261 - SAP HANA Database Backup Policy Recommendations and Regular Backup Script

    =====Symptom For SAP Business One, version for SAP HANA users, SAP HANA provides a range of database ...

  6. 23.模拟登录cookies请求速询网站数据

    采集速询网站数据: 网站地址:http://www.suxun0752.com/index.html 网站是需要账号登录才给返回信息的,我这里是直接拿的登录后的cookies请求的数据,cookies ...

  7. 33.scrapy采集网站表单数据

    这几天一直都再用scrapy写网站数据采集的爬虫,这里我就选一个写过的爬虫来记录一下. 杭州造价网:http://183.129.219.195:8081/bs/hzzjb/web/list 这里出现 ...

  8. 学习笔记:CommonJS规范、AMD规范

    CommonJS规范 http://wiki.jikexueyuan.com/project/webpack-handbook/commonjs.html CommonJS 规范 http://www ...

  9. 代码: html 页面小效果 (集合,待补充)

    标签切换(下部内容区跟着切换): 2016-6-2 <script type="text/javascript" src="http://cdn.bootcss.c ...

  10. Maven私服安装

    下载安装包:nexus(https://www.sonatype.com/download-oss-sonatype) 默认用户密码字符串: adminAdministratorUseractive& ...