Permutation Sequence leetcode java
题目:
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123""132""213""231""312""321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
题解:
发现数学规律。
首先先捋捋这道题要干啥,给了我们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次即可。
同时,为了可以跟数组坐标对其,令k先--。
代码如下:
1 public String getPermutation(int n, int k) {
2 k--;//to transfer it as begin from 0 rather than 1
3
4 List<Integer> numList = new ArrayList<Integer>();
5 for(int i = 1; i<= n; i++)
6 numList.add(i);
7
8 int factorial = 1;
9 for(int i = 2; i < n; i++)
factorial *= i;
StringBuilder res = new StringBuilder();
int times = n-1;
while(times>=0){
int indexInList = k/factorial;
res.append(numList.get(indexInList));
numList.remove(indexInList);
k = k%factorial;//new k for next turn
if(times!=0)
factorial = factorial/times;//new (n-1)!
times--;
}
return res.toString();
}
Reference:
http://blog.csdn.net/linhuanmars/article/details/22028697
http://blog.csdn.net/fightforyourdream/article/details/17483553
http://blog.csdn.net/u013027996/article/details/18405735
Permutation Sequence leetcode java的更多相关文章
- Permutation Sequence [LeetCode]
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Longest Consecutive Sequence leetcode java
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...
- 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]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence
一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
随机推荐
- Ubuntu 16.04 安装WPS
1.下载安装包和缺少的字体 安装包下载[http://wps-community.org/download.html?vl=a21#download](这里下载的是最新的安装包) 缺少的字体下载[链接 ...
- UEditor 的使用
UEditor 的使用 一.UEditor 的使用 官方网站:http://ueditor.baidu.com/website/ 下载地址:http://ueditor.baidu.com/websi ...
- BZOJ1171 : 大sz的游戏
f[i]=min(f[j])+1,线段j与线段i有交,且l[i]-l[j]<=L. 线段j与线段i有交等价于y[j]>=x[i],x[j]<=y[i]. 因为l[i]递增,所以可以维 ...
- Xtreme8.0 - Back to Square 1 数学
Back to Square 1 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/back-to- ...
- 整理c# 不常用但有用代码
# 整理c# 不常用但有用代码 1.winform窗体右键菜单打开其他窗体 private void contextMenuStripHandler_Click(object sender, Even ...
- 微服务架构的分布式事务解决方案 - zhaorui2017的博客 - CSDN博客
微服务架构的分布式事务解决方案 - zhaorui2017的博客 - CSDN博客 http://blog.csdn.net/zhaorui2017/article/details/7643679 ...
- hdu4467 Graph
Graph Problem Description P. T. Tigris is a student currently studying graph theory. One day, when h ...
- 使用Axure RP原型设计实践06,登录验证
登录验证主要功能包括: ● 用户名错误,提示无效用户名,用户名和密码文本框清空● 用户名存在,密码错误,提示密码错误,密码清空,焦点进入密码框● 用户名和密码都正确,验证通过 本篇接着"使用 ...
- iOS 各种控件默认高度(图示)
1.状态栏 状态栏一般高度为20像素,在打手机或者显示消息时会放大到40像素高,注意,两倍高度的状态栏在好像只能在纵向的模式下使用.如下图 用户可以隐藏状态栏,也可以将状态栏设置为灰色,黑色或者半透明 ...
- MongoDB:Map-Reduce
Map-reduce是一个考虑大型数据得到实用聚集结果的数据处理程式(paradigm).针对map-reduce操作,MongoDB提供来mapreduce命令. 考虑以下的map-reduce操作 ...