LintCode 388: Kth Permutation

题目描述

给定 nk,求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的更多相关文章

  1. LintCode 190: Next Permutation

    LintCode 190: Next Permutation 题目描述 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列. 如果没有下一个排列,则输出字典序最小的序列. 样例 ...

  2. lintcode:next permutation下一个排列

    题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...

  3. lintcode:previous permutation上一个排列

    题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...

  4. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  5. Lintcode388 Permutation Sequence solution 题解

    [题目描述] Given n and k, return the k-th permutation sequence. Notice:n will be between 1 and 9 inclusi ...

  6. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  7. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  9. 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 ...

随机推荐

  1. 0506Scrum项目1.0

    1.确定选题. 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 截止日期:2016.5.6日晚10点 团队名称:虫洞 团 ...

  2. PAT 1051 复数乘法

    https://pintia.cn/problem-sets/994805260223102976/problems/994805274496319488 复数可以写成(A + Bi)的常规形式,其中 ...

  3. vue开发完成后打包后图片路径不对

    用vue做了一个小的移动端项目,从头到尾做下来,感觉自己好多东西都没弄清楚过.也学到了很多,已整理笔记在自己电脑上,但是比较零散,空了再来仔细整理整理. 于是,上周五模拟好数据(接口还未写),准备打包 ...

  4. cxDBTreelist一些使用方法

    一.导出EXCEL   TXT   HTML: uses cxTLExportLink; cxExportTLToEXCEL(dm.SaveDialog.FileName,cxDBTreeList1, ...

  5. chdir

    <?php //获得当前目录 echo getcwd(); echo "<br />"; //改变为 images 目录 chdir("images&q ...

  6. Spring Cloud之Eureka服务注册与发现

    解决什么问题 ➟阐述微服务以及服务注册发现的部分概念 ➟阐述Eureka服务注册与发现的部分原理及细节 为什么需要服务中心 过去,每个应用都是一个CPU,一个主机上的单一系统.然而今天,随着大数据和云 ...

  7. Sql Server外键约束

    一.添加约束(级联删除) 1.创建表结构时添加 create table UserDetails(id int identity(1,1) primary key,name varchar(50) n ...

  8. C++ 查看预处理后的源文件(查看真实代码)

    gcc -E filename.cpp 会生成 filename.cpp 的预处理文件,这样就能看到宏展开后的代码,用于理解和调试宏非常有帮助.   http://www.qtdebug.com/cp ...

  9. 《统计学习方法》P179页10.22前向后向算法公式推导

  10. Mininet 系列实验(一)

    关于SDN的第一个实验,似乎实验室里的前辈们也都是从这里开始的. 实验内容 使用源码安装Mininet 参考 Mininet使用源码安装 实验环境 虚拟机:Oracle VM VirtualBox U ...