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. HDU 2086 A1 = ?

    http://acm.hdu.edu.cn/showproblem.php?pid=2086 Problem Description 有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci ( ...

  2. 3 vue-router 的默认hash 改mode:history去除#号 传参

    npm install vue-router --save      //安装 传参

  3. 优先级:P0

    优先级:P0 Priority https://www.zhihu.com/question/23353333?sort=created 一个团队, 每周需求pk的例会,每个产品经理都要给自己提出的需 ...

  4. npm 镜像修改

    1, 修改 下载仓库为淘宝镜像  npm config set registry http://registry.npm.taobao.org/ 2, 如果要发布自己的镜像需要修改回来  npm co ...

  5. JDBC数据库连接技术

    [学习笔记]JDBC数据库连接技术(Java Database Connectivity) 一.JDBC简介 Java是通过JDBC技术实现对各种数据库的访问的,JDBC是Java数据库连接技术的简称 ...

  6. Machine Learning CodeForces - 940F(带修改的莫队)

    题解原文地址:https://www.cnblogs.com/lujiaju6555/p/8468709.html 给数组a,有两种操作,1 l r查询[l,r]中每个数出现次数的mex,注意是出现次 ...

  7. Crawl(2)

    http://cuiqingcai.com/3179.html # *-* coding: UTF-8 *-* import urllib2 import cookielib import re im ...

  8. [洛谷P5216]DLS 采花

    题目大意:有$n$个数,任意排列,排列后第$i$个数会产生贡献当且仅当$1\sim i-1$中的数不是它的因子,问所有排列的贡献和 题解:发现一个数要产生贡献要求所有它的因子在它的右边,设有$cnt_ ...

  9. tarjan解决路径询问问题

    好久没更新了,就更一篇普及组内容好了. 首先我们考虑如何用tarjan离线求出lca,伪代码大致如下: def tarjan(x): 将x标记为已访问 for c in x的孩子: tarjan(c) ...

  10. 【bzoj1087】互不侵犯King

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...