LintCode 388: Kth Permutation
LintCode 388: Kth Permutation
题目描述
给定 n
和 k
,求123..n
组成的排列中的第 k
个排列。
样例
对于 n = 3
, 所有的排列如下:
123
132
213
231
312
321
如果 k = 4
, 第4
个排列为231
.
Wed Mar 1 2017
思路
这道题很明显就不用什么算法呀,直接用除法算一下就好了。
为了取整方便,把题目中的 \(k\) 从 \(1\) 开始计数,改成从 \(0\) 开始计数,即 \(k = k - 1\) 即可。
对于 \(n\) 个数,第 \(i\) 位数有 \(n - i + 1\) 个可选的数字\((1 \leq i \leq n)\),这个数字之后的排列情况有 \((n - i)!\) 种。
所以在待选数字集中第 \(\frac{k-1}{(n-1)!}\) 个数字就是应该放在第 \(i\) 位上。
代码
// 第k个排列
class Solution {
public:
/**
* @param n: n
* @param k: the kth permutation
* @return: return the k-th permutation
*/
int fact(int k)
{
if (k <= 1) return 1;
return k * fact(k - 1);
}
string getPermutation(int n, int k)
{
string s;
string* ans = new string();
for (int i = 1; i <= n; ++i)
s.push_back('0' + i);
--k;
for (int i = 1; i <= n; ++i)
{
int f = fact(n - i);
int p = k / f;
k -= p * f;
ans->push_back(s[p]);
s.erase(s.begin() + p);
}
return *ans;
}
};
LintCode 388: Kth Permutation的更多相关文章
- LintCode 190: Next Permutation
LintCode 190: Next Permutation 题目描述 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列. 如果没有下一个排列,则输出字典序最小的序列. 样例 ...
- lintcode:next permutation下一个排列
题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...
- lintcode:previous permutation上一个排列
题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- Lintcode388 Permutation Sequence solution 题解
[题目描述] Given n and k, return the k-th permutation sequence. Notice:n will be between 1 and 9 inclusi ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- 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 序列排序
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 ...
随机推荐
- form表单转化json对象
利用 $.fn 可以让每一个jquery 对象都能直接使用这个方法. //form表单转化json对象$.fn.serializeObject = function () { var o = {}; ...
- sql中详解round(),floor(),ceiling()函数的用法和区别?
round() 遵循四舍五入把原值转化为指定小数位数,如:round(1.45,0) = 1;round(1.55,0)=2floor()向下舍入为指定小数位数 如:floor(1.45,0)= 1; ...
- Flexpod的开关机顺序
Flexpod = Cisco UCS + Cisco Nexus + Netapp (中文名叫做“融合基础架构”) 之前没有接触过这套系统,不太明白怎么个开关机的顺序,借公司停电的机会实际演练了一番 ...
- SpringBoot(九)_springboot集成 MyBatis
MyBatis 是一款标准的 ORM 框架,被广泛的应用于各企业开发中.具体细节这里就不在叙述,大家自行查找资料进行学习下. 加载依赖 <dependency> <groupId&g ...
- [BZOJ2095][Poi2010]Bridges 最大流(混合图欧拉回路)
2095: [Poi2010]Bridges Time Limit: 10 Sec Memory Limit: 259 MB Description YYD为了减肥,他来到了瘦海,这是一个巨大的海, ...
- bzoj 2124 等差子序列 (线段树维护hash)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 1922 Solved: 714[Submit][Status][Discuss ...
- STL Set和multiset 容器
STL Set和multiset 容器 set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列. 元素插入过程是按排序规则插入,所以不能指定插入位 ...
- c++11 闭包的实现
c++11 闭包的实现 什么是闭包 闭包有很多种定义,一种说法是,闭包是带有上下文的函数.说白了,就是有状态的函数.更直接一些,不就是个类吗?换了个名字而已. 一个函数,带上了一个状态,就变成了闭包了 ...
- BZOJ 2844: albus就是要第一个出场
2844: albus就是要第一个出场 Time Limit: 6 Sec Memory Limit: 128 MBSubmit: 1134 Solved: 481[Submit][Status] ...
- Java之Stream流
Stream流的初步学习 初次学习Stream流的学习笔记,学习之前先了解一下函数式接口 概述 API是一个程序向使用者提供的一些方法,通过这些方法就能实现某些功能.所以对于流API来 说,重点是怎么 ...