【LeetCode】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.
提示:
这道题我一上来使用了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的更多相关文章
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode】060. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 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 ...
- 【LeetCode】567. Permutation in String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...
- 【leetcode】Next Permutation
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
随机推荐
- MyEclipse2016添加外部的maven插件
1.在maven官网下载最新的maven安装包,下载地址:http://maven.apache.org/download.cgi: 2.目前maven的版本是3.5.0,我们下载apache-mav ...
- Java学习笔记——设计模式之三.装饰模式
函数应该做一件事,做好这件事,只做这一件事. --Clean Code 装饰模式,上代码: 先定义零件类: package cn.no3.decorator.template; public abst ...
- DNN个性化推荐模型
0 推荐技术 1)协同过滤: (1)基于user的协同过滤:根据历史日志中用户年龄,性别,行为,偏好等特征计算user之间的相似度,根据相似user对ite ...
- Document 对象
document.body //返回文档的body元素 document.cookies //返回当前文档有关的所有cookie document.createElement() //创建元素节点 d ...
- cassandra.yaml 配置 (非原创,侵删)
Copy from: http://blog.csdn.net/y_h_t/article/details/11917531 Cassandra中所有的运行配置都是在配置文件cassandra.yam ...
- 消息队列RabbitMQ与Spring集成
1.RabbitMQ简介 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现. 官网:http://www.rabbitmq.c ...
- Introducing: Machine Learning in R(转)
Machine learning is a branch in computer science that studies the design of algorithms that can lear ...
- springboot redis 缓存对象
只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下 gradle加入依赖 compile(" ...
- AbstractHandlerMapping解读
一.AbstractHandlerMapping简介 AbstractHandlerMapping是HandlerMapping的抽象实现,所有的HandlerMapping都继承自AbstractH ...
- 《NLTK基础教程》译者序
购买<NLTK基础教程> 说来也凑巧,在我签下这本书的翻译合同时,这个世界好像还不知道AlphaGo的存在.而在我完成这本书的翻译之时,Master已经对人类顶级高手连胜60局了.至少从媒 ...