给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。
按大小顺序列出所有排列情况,并一一标记,
可得到如下序列 (例如,  n = 3):
   1."123"
   2. "132"
   3. "213"
   4. "231"
   5. "312"
   6. "321"
给定 n 和 k,返回第 k 个排列序列。
注意:n 介于1到9之间(包括9)。
详见:https://leetcode.com/problems/permutation-sequence/description/

Java实现:

在数列 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次即可。同时,为了可以跟数组坐标对其,令k先--。

class Solution {
public String getPermutation(int n, int k) {
k--;//to transfer it as begin from 0 rather than 1 List<Integer> numList = new ArrayList<Integer>();
for(int i = 1; i<= n; i++){
numList.add(i);
} int fac = 1;
for(int i = 2; i < n; i++) {
fac *= i;
} StringBuilder res = new StringBuilder();
int times = n-1;
while(times>=0){
int indexInList = k/fac;
res.append(numList.get(indexInList));
numList.remove(indexInList); k = k%fac;//new k for next turn
if(times!=0){
fac = fac/times;//new (n-1)!
}
times--;
} return res.toString();
}
}

参考:https://www.cnblogs.com/springfor/p/3896201.html

060 Permutation Sequence 排列序列的更多相关文章

  1. 【LeetCode每天一题】Permutation Sequence(排列序列)

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

  2. Java for LeetCode 060 Permutation Sequence

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

  3. 【LeetCode】060. Permutation Sequence

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

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

  5. [LeetCode] Permutation Sequence 序列排序

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

  6. [Swift]LeetCode60. 第k个排列 | Permutation Sequence

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

  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] 60. Permutation Sequence 序列排序

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

  9. 60. Permutation Sequence(求全排列的第k个排列)

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

随机推荐

  1. druid相关的时间序列数据库——也用到了倒排相关的优化技术

    Cattell [6] maintains a great summary about existing Scalable SQL and NoSQL data stores. Hu [18] con ...

  2. eclipse的maven工程Dynamic Web Module 2.3 修改为3.0 解决办法

    1. 创建Maven Web工程 2. 项目只有src/main/resources >Java Build Path导入Tomcat运行环境 3. 删除以图片红框中的文件 4. Propert ...

  3. ACM学习历程—BestCoder 2015百度之星资格赛1004 放盘子(策略 && 计算几何)

    Problem Description 小度熊喜欢恶作剧.今天他向来访者们提出一个恶俗的游戏.他和来访者们轮流往一个正多边形内放盘子.最后放盘子的是获胜者,会赢得失败者的一个吻.玩了两次以后,小度熊发 ...

  4. 关于Tensorflow 加载和使用多个模型的方式

    在Tensorflow中,所有操作对象都包装到相应的Session中的,所以想要使用不同的模型就需要将这些模型加载到不同的Session中并在使用的时候申明是哪个Session,从而避免由于Sessi ...

  5. hdu 3625 Examining the Rooms —— 第一类斯特林数

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3625 学习斯特林数:https://blog.csdn.net/qq_33229466/article/d ...

  6. 洛谷 P4512 [模板] 多项式除法

    题目:https://www.luogu.org/problemnew/show/P4512 看博客:https://www.cnblogs.com/owenyu/p/6724611.html htt ...

  7. Lagom学习(一)

    Lagom是JAVA系下响应式 微服务框架,其特性包括: 目前,大多数已有的微服务框架关注于简化单个微服务的构建,Lagom将其扩展到了微服务所构成的系统,分布式系统的复杂性. 同步通信使用HTTP, ...

  8. sgu 321 The Spy Network (dfs+贪心)

    321. The Spy Network Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: standard ...

  9. linux下安装mysql的三种方法:rpm包安装、yum安装、源码包安装

    1 安装MySQL数据库服务器安装方法一://查询系统自带的数据库rpm -qa | grep -i mysql //卸载查询到的所有mysqlrpm -e --nodeps mysql-libs-5 ...

  10. win7 卸载ie10+ 重新安装ie8

    烦恼: erp系统不支持高版本ie10+ 项目开发测试需要安装了高版本ie 项目结束,为了方便使用erp,决定卸载ie11,重新安装ie8 解决方法: 1.win+R打开运行命令,键入appwiz.c ...