060 Permutation Sequence 排列序列
给出集合 [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 排列序列的更多相关文章
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- 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 ...
- 【LeetCode】060. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 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 ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [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 ...
- Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [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 ...
- 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 ...
随机推荐
- 「LOJ#10036」「一本通 2.1 练习 2」Seek the Name, Seek the Fame (Hash
题目描述 原题来自:POJ 2752 给定若干字符串(这些字符串总长 ≤4×105 \le 4\times 10^5 ≤4×105),在每个字符串中求出所有既是前缀又是后缀的子串长度. 例如:abab ...
- poj2356Find a multiple——鸽巢定理运用
题目:http://poj.org/problem?id=2356 N个数,利用鸽巢定理可知应有N+1个前缀和(包括0),因此其%N的余数一定有重复: 同余的两个前缀和之差一定为N的倍数,据此得出答案 ...
- 微信小程序再次升级:卖货小店小程序不用开发也能进行交易
卖货小店小程序,不用开发一行代码也能帮商家实现交易功能,这个真是几家欢喜几家愁啊,对于开发小程序商城的公司来说,这个无疑是一个雷霆之际,第一反应就是,这下完了,小程序自身就支持交易,那还要我们这些第三 ...
- TPS与QPS
一.TPS:Transactions Per Second(每秒传输的事物处理个数),即服务器每秒处理的事务数.TPS包括一条消息入和一条消息出,加上一次用户数据库访问.(业务TPS = CAPS × ...
- spring boot 学习三:OAuth2 认证
1: 代码地址: https://github.com/liufeiSAP/uaa-zuul 2: 安装: postgres 下载 https://www.openscg.com/bigsq ...
- python 进行web测试
1:安装nosetests Python 单元测试框架之Nose http://blog.sina.com.cn/s/blog_65a8ab5d0101fihb.html Python nose te ...
- 如何查看Mysql event事件是否启用
mysql> show variables like 'event_scheduler';+-----------------+-------+| Variable_name | Value ...
- Elasticsearch5.X Mapping详解
0.引言 在关系型数据库如Mysql中,设计库表需要注意的是: 1)需要几个表: 2)每个表有哪些字段: 3)表的主键及外键的设定——便于有效关联. 表的设计遵守范式约束,考虑表的可扩展性,避免开发后 ...
- ADT-Bundle--Android开发环境快速搭建
http://blog.csdn.net/aizquan/article/details/8974750
- 2015年第六届蓝桥杯国赛试题(JavaA组)
1.结果填空 (满分15分)2.结果填空 (满分35分)3.代码填空 (满分31分)4.程序设计(满分41分)5.程序设计(满分75分)6.程序设计(满分103分) 1.标题:胡同门牌号 小明家住在一 ...