Permutation Sequence [LeetCode]
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.
Solutions: Caculates the index of the first number by idx = (k -1) / (n-1)! , and the new k and new n, then goes to next round.
string getPermutation(int n, int k) {
vector<int> nums;
vector<int> factors(,);
for(int i = ; i <= n; i++){
nums.push_back(i);
factors.push_back(factors[i - ] * i);
} string ret;
while(k > && nums.size() > ) {
int idx = (k -) / factors[n -];
ret.push_back(nums[idx] + );
nums.erase(nums.begin() + idx);
k = k - idx * factors[n - ];
n --;
} return ret;
}
Permutation Sequence [LeetCode]的更多相关文章
- Permutation Sequence leetcode java
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence
一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- [leetcode]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
随机推荐
- DevExpress所有的窗体,使用同一款皮肤
https://www.devexpress.com/Support/Center/Question/Details/K18516 To accomplish your task, please ex ...
- [SAP ABAP开发技术总结]数据引用(data references)、对象引用(object references)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- VS2008中开发智能设备程序的一些总结
原文链接:http://blog.csdn.net/citybug_nj/article/details/2598705 程序中包括四个部分: 系统配置 这个部分用来配置系统中的相关参数,参数包括数据 ...
- python_way day12 sqlalchemy,原生mysql命令
python_way day12 sqlalchemy,mysql原生命令 1.sqlalchemy 2.mysql 原生命令 一,sqlalchemy SQLAlchemy本身无法操作数据库,其必 ...
- python_way ,json(自学)
python_way ,json 如果我们想将多行字典存放到文件中,并且还需要调出这些字典继续使用那么就要是用json. 首先将字典用json转换成字符串,存放到文件中. a = {"tel ...
- Perfection Kills
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- UpdatePanel的简单用法(转)
微软AJAX虽然是过时的玩意,但是得维护公司之前的老项目,转载看看. 局部更新是ajax技术的最基本,也是最重要的用法,今天大概把asp.net ajax中的局部更新控件 updatepanel的用法 ...
- css处理浏览器兼容问题
浏览器的兼容: _ ie6认识 格式:_width:100px;要写在最后面来覆盖前面的. * ie6和ie7都认识 格式:* width:100px;要写在最后面来覆盖前面的. \0 ie8认识 ...
- 通过注解(annotation)配置Bean
Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...
- css 常用样式命名规则
大家在写css的时候,对一些html标签起一个合适的名字是个很头疼的事情,现在给大家分享项目中常用的名字供参考. 外套:wrap ——用于最外层 头部:header ——用于头部 主要内容:mai ...