60 Permutation Sequence
60 Permutation Sequence
题目
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.
解析
- 这题不可以用递归的解法,否则不管怎么优化都会超时,这题时间卡得很紧啊。。。之前一直在优化递归,从几百毫秒优化到 9 毫秒依然不行。遂放弃,上网学习大神的解法,才发现这题是有数学技巧的。
- 具体来说是:n 个数字有 n!种全排列,每种数字开头的全排列有 (n - 1)!(n!/ n)种。所以用 k / (n - 1)! 就可以得到第 k 个全排列是以第几个数字开头的。用 k % (n - 1)! 就可以得到第 k 个全排列是某个数字开头的全排列中的第几个。这又变成了最初的问题设置。
- 对于以某个数字开头的全排列(第一个数字固定后的全排列,不再理会第一个数字),它有 (n - 1)! 种全排列,那么这些全排列中,每个数字开头的全排列有 (n - 2)! ((n-1)! / (n-1))。
依次类推。
// 60. Permutation Sequence
class Solution_60 {
public:string getPermutation(int n, int k) {vector<int> vec;for (int i = 0; i < n;i++){vec.push_back(i + 1);}for (int i = 0; i < k-1;i++){next_permutation(vec.begin(), vec.end());}string res;for (int i = 0; i < vec.size();i++){char temp = vec[i] + '0';res.push_back(temp);}return res;}string getPermutation_ref(int n, int k) {vector<int> permutation(n + 1, 1);for (int i = 1; i <= n; ++i) {permutation[i] = permutation[i - 1] * i;}vector<char> digits = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };int num = n - 1;string res;while (num) {int t = (k - 1) / (permutation[num--]);k = k - t * permutation[num + 1];res.push_back(digits[t]);digits.erase(digits.begin() + t);}res.push_back(digits[k - 1]);return res;}};
题目来源
- 60 Permutation Sequence
- 【LeetCode】60. Permutation Sequence
60 Permutation Sequence相关推荐
- 【数字全排列】LeetCode 60. Permutation Sequence
LeetCode 60. Permutation Sequence Solution0: 偷鸡摸狗的做法 class Solution {public:string getPermutation(in ...
- 60. Permutation Sequence
一.题目 1.审题 2.分析 给两个数字 n 与 k,返回 1-n 所有数字组成的从小到大的全排序的第 k 个数. 二.解答 1.思路: 方法一.采用字典序列,返回全部序列后,输出第 k 个.(时间超 ...
- Permutation Sequence
2019独角兽企业重金招聘Python工程师标准>>> The set [1,2,3,-,n] contains a total of n! unique permutations. ...
- LeetCode60:Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [Swift]LeetCode60. 第k个排列 | Permutation Sequence
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 康拓编码——Permutation Sequence
描述 2.1.13 Permutation Sequence The set [1,2,3,ĉ ,n] contains a total of n! unique permutations. By l ...
- LeetCode Permutation Sequence(求排列中的第k个排列是什么)
思路:使用阶乘,f(n) = f(n -1) * n,在确定第i个数是,用k/f(n - 1 - i) 代码如下: public class Solution {public String getP ...
- Leetcode Permutation Sequence
超时算法-排序dfs: class Solution { public: string s; string s1; int count;string getPermutation(int n, int ...
- LeetCode刷题(35)--Permutation Sequence
对于第一个数字,剩余共有(n-1)!中排列,因此第一个数字为k/(n-1)!,之后的数字依次类推 import math class Solution(object):def getPermutati ...
最新文章
- [UE4]死亡后调整视角
- python if __name__ == '__main__'
- DevExpress的DateEdit设置显示日期和时间
- mysql 正则表达式_MySQL(基础正则表达式)
- HTML 介绍及标签
- git submodule 的使用
- jquery中如何表达本页网址_jquery中怎么跳转页面?
- 《想吃麻花现给你拧》
- 波卡二层扩容协议Plasm Network发布v1.7.0-dusty版本
- rabbitmq实战指南_太香了这份架构解密:从分布式到微服务(第二版),神仙进阶指南...
- Json API接口数据生成
- 阶段3 2.Spring_03.Spring的 IOC 和 DI_13 课程知识梳理
- 超好用的录屏软件 captura
- 英语论文引用中的常用缩写含义
- 《CSS世界》学习笔记(一)
- Top K Frequent Elements
- 特征工程——特征选择
- 自制迷宫小游戏 :O 的冒险
- 安卓从入门到进阶第一篇(环境搭建)
- 分布式-分布式常见问题和解决方案