【题目描述】

Given and k, return the k-th permutation sequence.

Notice:will be between 1 and 9 inclusive.

给定nk,求123..n组成的排列中的第k个排列。

【注】1 ≤ n ≤ 9

【题目链接】

www.lintcode.com/en/problem/permutation-sequence/

【题目解析】

这道题给了我们n还有k,在数列 1,2,3,... , n构建的全排列中,返回第k个排列。

题目告诉我们:对于n个数可以有n!种排列;那么n-1个数就有(n-1)!种排列。

那么对于n位数来说,如果除去最高位不看,后面的n-1位就有(n-1)!种排列。

所以,还是对于n位数来说,每一个不同的最高位数,后面可以拼接(n-1)!种排列。

所以你就可以看成是按照每组(n-1)!个这样分组。

利用 k/(n-1)! 可以取得最高位在数列中的index。这样第k个排列的最高位就能从数列中的index位取得,此时还要把这个数从数列中删除。

然后,新的k就可以有k%(n-1)!获得。循环n次即可。

【参考答案】

www.jiuzhang.com/solutions/permutation-sequence/

Lintcode388 Permutation Sequence solution 题解的更多相关文章

  1. 【leetcode刷题笔记】Permutation Sequence

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

  2. 【LeetCode】060. Permutation Sequence

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  3. Permutation Sequence

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

  4. [LeetCode] Permutation Sequence 序列排序

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

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

  6. 【leetcode】 Permutation Sequence (middle)

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

  7. [Leetcode] 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] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

  9. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

随机推荐

  1. C#编写一个大字母游戏,详细代码,不懂问博主。。。。

    using System; using System.Drawing; using System.Windows.Forms; using System.Media; namespace dazimu ...

  2. 事后诸葛亮分析——Beta版本

    事后诸葛亮分析 请两个小组在Deadline之前,召开事后诸葛亮会议,发布一篇事后分析报告. 软件工程课的目的,主要是让大家通过做项目,学到软件工程的知识,而不是低水平重复. 软件=程序+软件工程,软 ...

  3. 敏捷冲刺(Beta版本)

    评分基准: 按时交 - 有分(计划安排-10分,敏捷冲刺-70分),检查的项目包括后文的三个个方面 冲刺计划安排(单独1篇博客,基本分5分,根据完成质量加分,原则上不超过满分10分) 七天的敏捷冲刺( ...

  4. C语言第三次作业--嵌套循环

    一.PTA实验作业 题目1:硬币数 1. 本题PTA提交列表 2. 设计思路 步骤一:定义整型变量fen5,fen2,fen1,表示1分2分和5分,零钱数额x,总硬币数total,换法count 步骤 ...

  5. Java的HelloWorld程序

    Java的HelloWorld程序 1.新建文本文档,编写HelloWorld程序,最后保存时记得保存成.java格式 2.在D盘新建一个HelloJava文件夹用于保存java程序 3.使用WIN+ ...

  6. centos7 安装docker

    1.首先cent7 基本是在vm上完全安装'. 2.参考官方网站安装 1.https://wiki.centos.org/AdditionalResources/Repositories OS req ...

  7. Mego开发文档 - 事务

    事务 事务允许以原子方式处理多个数据库操作.如果事务已提交,则所有操作都已成功应用于数据库.如果事务回滚,则没有任何操作应用于数据库. 默认行为 默认情况下,如果数据库提供程序支持事务,则单次的提交操 ...

  8. JMM简介

    JMM:Java Memory Model(Java内存模型),围绕着在并发过程中如何处理可见性.原子性.有序性这三个特性而建立的模型. 可见性:JMM提供了volatile变量定义.final.sy ...

  9. tomcat 修改默认字符集

    找到connector节点,插入 disableUploadTimeout="true" useBodyEncodingForURI="true" URIEnc ...

  10. SpringCloud的注解:汇总篇

    使用注解之前要开启自动扫描功能,如下配置中base-package为需要扫描的包(含子包): 1 <context:component-scan base-package="cn.te ...