题目:

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.

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

提示:

这道题我一上来使用了backtracking的方法依次构造出排列数,当然结果不出所料的TLE了。实际上,仔细观察这些数字,我们还是不难发现一些规律的。

假设有四位数字{1, 2, 3, 4},那么他们能够产生的排列数是什么呢?

  • 1 + {2, 3, 4}
  • 2 + {1, 3, 4}
  • 3 + {1, 2, 4}
  • 4 + {1, 2, 3}

其实就是选定第一位数字后,其他剩下的数字进行排列组合,就能求出以该数字打头的所有排列组合。想必已经能发现一些规律了,我们干脆再举一个具体的例子,比如我们现在想要找第14个数,那么由于14 = 6 + 6 + 2。因此第一个数打头的是3,然后再求{1, 2, 4}中第二个排列组合数,答案是"142"。所以最终答案就是"3142"啦。

这里有一些问题是需要我们注意的:

  • 构造排列数从最高位开始,当选出一个数字后,就应当把这个数字erase掉,防止后面又出现;
  • 我们所要求的第k个数需要在每次循环中减去对应的值;
  • 注意程序中的数组是从0开始的,但题目的输入是从1开始计数的。

代码:

class Solution {
public:
string getPermutation(int n, int k) {
vector<int> permutation(n + , );
for (int i = ; i <= n; ++i) {
permutation[i] = permutation[i - ] * i;
}
vector<char> digits = { '', '', '', '', '', '', '', '', '' };
int num = n - ;
string res;
while (num) {
int t = (k - ) / (permutation[num--]);
k = k - t * permutation[num + ];
res.push_back(digits[t]);
digits.erase(digits.begin() + t);
}
res.push_back(digits[k - ]);
return res;
}
};

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

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

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

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

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

  3. 【LeetCode】060. Permutation Sequence

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

  4. LeetCode OJ 60. Permutation Sequence

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

  5. 【LeetCode】567. Permutation in String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...

  6. 【leetcode】Next Permutation

    Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...

  7. 【leetcode】Longest Consecutive Sequence(hard)☆

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. 【leetcode】Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

随机推荐

  1. 使用crontab,让linux定时执行shell脚本

    阅读目录 1. cron服务[Ubuntu环境] 2. crontab用法 3. 编辑crontab文件 4. 流程举例 5. 几个例子 Linux中,周期执行的任务一般由cron这个守护进程来处理. ...

  2. 从编译器角度理解C++中的引用和指针

    欲分析指针和引用,则要分析变量名和地址之间的关系(不管你理解还是不理解,无论你是从老师那里听到的,还是网上看到的,应该都知道两句话:1. 指针就是地址,2.引用就是给变量起个别名) 所以我们就要来分析 ...

  3. Java实现的高效计数器

    本文转载地址:            http://blog.csdn.net/snarlfuture/article/details/17049731 在统计来自数据库或文本中某些内容的频率时,你可 ...

  4. swift MBProgressHUD加载gif或者apng的动图

    效果图 给MBProgressHUD添加一个分类(extension) extension MBProgressHUD { /// MBProgressHUD 显示加载gif hud方法 /// // ...

  5. springboot 1.5.2 集成kafka 简单例子

    添加依赖 compile("org.springframework.kafka:spring-kafka:1.1.2.RELEASE") 添加application.propert ...

  6. mybatis xml配置文件要点说明

    mapper映射方式: 1 一一具体列举的方式 2扫描package 如: <mappers> <!-- 告知映射文件方式1,一个一个的配置 <mapper resource= ...

  7. Java经典编程题50道之一

    有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? public class Example01 {    publi ...

  8. 《NLTK基础教程》译者序

    购买<NLTK基础教程> 说来也凑巧,在我签下这本书的翻译合同时,这个世界好像还不知道AlphaGo的存在.而在我完成这本书的翻译之时,Master已经对人类顶级高手连胜60局了.至少从媒 ...

  9. PHP Laravel 环境与框架结构

    MAMP: 开发环境   php+apach+mysqlLaravel: 框架5.2phpstorm: 工具IDE目录结构:app:运行 核心代码bootstrap : 框架启动,加载config: ...

  10. 如何通过JS实现简单抖动效果

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...