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.
分析: 这道题目看似可以采用全排列的方式得到所有组合,之后排序筛选出第k个大小的组合,但是会超出时间,仔细考虑题目中只要求第k个大小的组合,假设n=4, k=7; 4个数共有4x3x2x1 24个组合,考虑排在第k=7的组合的第一个数,应该是2, 因为第一个数可能为1,2,3,4中的任何一个,每一个情况都有3x2x1 6种情况, 所以向上取整7/6 为2, 这样第一个数就可以确定了,第二个数的情况排除第一个数, 考虑1,3,4 中第 k-(k/6) 中第一个数就可以了,依次类推
class Solution {
public:
int factorial(int n){
if(n== || n==)
return ;
int res=;
for(int i =; i<=n;i++)
res*=i;
return res;
}
char helper(string& s, int& k){
int n = factorial(s.size()-);
int index = (k-)/n;
char res = s[index];
s.erase(index,);
k -= n*index;
return res;
}
string getPermutation(int n, int k) {
string str = string("").substr(,n);
string res(n,' ');
for(int i=; i<n; i++)
res[i] =helper(str,k);
return res;
}
};
Permutation Sequence的更多相关文章
- [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 60. 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 (middle)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 60. Permutation Sequence
题目: 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):求指定位置的全 ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
- 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 ...
- Permutation Sequence [LeetCode]
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- Chrome调试中的奇技淫巧
网上有关Chrome调试的文章一搜一大堆,本文主要记录一下自己平时经常用并且又比较冷门的一些技巧. 打开Chrome调试工具 1.打开控制台的情况下,长按页面的“刷新”按钮可以选择按何种方式刷新(有正 ...
- DotLiquid模板引擎简介
DotLiquid是一个在.Net Framework上运行的模板引擎,采用Ruby的Liquid语法,这个语法广泛的用在Ruby on rails和Django等网页框架中. DotLiquid相比 ...
- “全能”选手—Django 1.10文档中文版Part1
本文是博主翻译的Django1.10版本官方文档的第一部分,如时间充裕,争取一直翻译下去,经验不足,或有错漏,敬请指正. 另外对于公开文档进行翻译的版权问题不是很清楚,如有侵权请联系我! 另外,要转载 ...
- LinqToDB 源码分析——前言
记得笔者进入公司的时候接触的第一个ORM框架是Entity Framework.为了Entity Framework也看了不些的英文资料(不是笔者装B哦).正式使用三个月后.笔者对他有一个全面性的认识 ...
- 微服务(Microservices)—Martin Fowler【翻译】
本文转载自:http://www.cnblogs.com/liuning8023/p/4493156.html -------------------------------------------- ...
- [Asp.net 5] Caching-缓存架构与源码分析
首先奉献caching的开源地址[微软源码] 1.工程架构 为了提高程序效率,我们经常将一些不频繁修改,但是使用了还很大的数据进行缓存.尤其是互联网产品,缓存可以说是提升效率优化第一利器.微软为我们实 ...
- EntityFramework中的DbContext使用疑点说明
1.DbContext怎么在Asp.mvc中使用? public class Repository { //实例化EF容器:有弊端.一个线程里可能会创建多个DbContext //DbContext ...
- 【Asp.Net Core】二、添加控制器和视图
控制器Controller 在添加控制器前,我们先看下它为我们自动生成的一些Controller,我们看下AccountController.cs 来看下登录验证方法Login async这个应该是异 ...
- String类
字符串的功能 A:判断功能 boolean equals(Object obj)//比较对象 boolean eq ...
- 【转载】10 个实用技巧,让 Finder 带你飞
来自:http://sspai.com/27403/ Finder 是 Mac 电脑的系统程序,有的功能类似 Windows 的资源管理器.它是我们打开 Mac 首先见到的「笑脸」,有了它,我们可以组 ...