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. mysql group by分组查询错误修改

    select @@global.sql_mode;set @@sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR ...

  2. JS计算两个日期之间的天数,时间差计算

    1.日期之间的天数计算 //计算天数差的函数,通用 function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2017-9-25格式 var aDate, ...

  3. javascript 组件化(转载)

    这边只是很简陋的实现了类的继承机制.如果对类的实现有兴趣可以参考我另一篇文章javascript oo实现 我们看下使用方法: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  4. 修改shell命令的history记录个数

    修改history记录的命令如下所示:# vi /etc/profile 找到histsize=1000,将其改为histsize=100(这条可根据实际情况而定). 不重启系统就可让其生效,如下所示 ...

  5. Beta阶段团队项目开发篇章3

    例会时间 2016.12.6晚 例会照片 个人工作 上阶段任务验收 中英文切换功能已经实现,调查结果分析已经完成,博客基本撰写完成,在征求其他组员意见后发布.任务基本完成. 任务分配 组员 任务内容 ...

  6. sql中详解round(),floor(),ceiling()函数的用法和区别?

    round() 遵循四舍五入把原值转化为指定小数位数,如:round(1.45,0) = 1;round(1.55,0)=2floor()向下舍入为指定小数位数 如:floor(1.45,0)= 1; ...

  7. 一键轻松查看apk包名和Main Activity

    环境 Windows系统(我的是Win10 64位) Python3(我的是3.6.1) 已安装Git 安装 pip install git+https://github.com/codeskyblu ...

  8. 使用Fiddler重定向App的网络请求

    前言 开发中手机app只能访问生产环境地址,所以给开发调试带来不便,可以通过Fiddler的代理和urlreplace方式解决. 步骤 1.开启Fiddler的代理模式Tools->Teleri ...

  9. Delphi报的错误

    引入单元时提示Unit 'Unit1' already uses all the units in the project. 可能是没有添加环境变量造成的,需要手动输入代码引用单元. 和Environ ...

  10. 深入理解JAVA虚拟机阅读笔记2——垃圾回收

    线程私有的程序计数器.虚拟机栈和本地方法栈随线程而生,随线程而灭.栈中的栈帧随方法的进入和退出有条不紊的入栈和出栈. 而Java堆和方法区因为需要多大内存.创建多少对象都是不确定的,因此这两个区域是垃 ...