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. cxGrid 单元格回车移到下一行,当移到最后一个单元格时回车新增一行【转】

    1 在TcxGridDBTableView中,设定属性 NewItemRow.Visible = True 2 在cxgrid中输入数据怎样回车换行  在TcxGridDBTableView中  将属 ...

  2. LR_问题_虚拟用户以进程和线程模式运行的区别

    进程方式和线程方式的优缺点: 如果选择按照进程方式运行, 每个用户都将启动一个mmdrv进程,多个mmdrv进程会占用大量内存及其他系统资源,这就限制了可以在任一负载生成器上运行的并发用户数的数量,因 ...

  3. node入门学习(二)

    一.模块系统 1.创建模块和引用模块 //如何创建一个模块 exports.hello = function(){ console.log('hello worl'); }; //这创建了一个模块 / ...

  4. ubuntu下安装软件的三种方法

    在ubuntu当中,安装应用程序常用的三种方法,分别是apt-get,dpkg安装deb和make install安装源码包三种. apt-get方法 使用apt-get install来安装应用程序 ...

  5. 对比数据库字段不同的sql (mysql版)

    -- 使用test库 `test_project_management` `oel_project_management` USE test; -- 旧表 DROP TABLE old_column_ ...

  6. [BZOJ3230] 相似字串 后缀数组+RMQ

    3230: 相似子串 Time Limit: 20 Sec  Memory Limit: 128 MB Description Input 输入第1行,包含3个整数N,Q.Q代表询问组数.第2行是字符 ...

  7. 【BZOJ4869】【SHOI2017】相逢是问候

    Description BZOJ传送门 Solution 这题涉及到指数嵌套堆叠,可能可以用欧拉函数解决. 试想一个数\(a_i\)经过\(k\)次操作后会变成什么? \[ k个c\;\; \begi ...

  8. 【bzoj1031】 JSOI2007—字符加密Cipher

    http://www.lydsy.com/JudgeOnline/problem.php?id=1031 (题目链接) 题意 给出一个字符串,求它的加密串. Solution 很显然,将串倍长后求它的 ...

  9. Java之字节数组和字符串的转换问题

    今天在使用字节流复制图片的时候发现一个问题,就是将字节数组转化为字符串的时候会出现长度不同的问题.这其实是个错误的操作. public static void main(String[] args) ...

  10. CDOJ--1056

    原题链接:http://acm.uestc.edu.cn/problem.php?pid=1056 题目:大小写切换 分析:以前这种问题我都是用dp写的,最近学到了一种更简洁的方法,特此记录下来! # ...