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 ...
随机推荐
- Xamarin iOS教程之视图显示图像
Xamarin iOS教程之视图显示图像 Xamarin iOS显示图像 在主视图中显示一个图像,可以让开发者的应用程序变的更有趣,例如,在一些应用程序开始运行时,都会通过图像来显示此应用程序的玩法或 ...
- Java 操纵XML之创建XML文件
Java 操纵XML之创建XML文件 一.JAVA DOM PARSER DOM interfaces The DOM defines several Java interfaces. Here ar ...
- BZOJ 1009 HNOI 2008 GT考试 递推+矩乘
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3679 Solved: 2254[Submit][Statu ...
- Slickflow.NET 开源工作流引擎基础介绍(八) -- 自动化任务调度实现介绍
前言:审批流程中常见的都是人工类型任务,但是也会有一些自动化的任务需要定时触发.因此,引擎框架中需要解决掉两个问题:选择合适的任务调度框架,集成新的任务调度模块. 1. 任务调度框架选择 Hangfi ...
- 将Azure WebSite(应用)集成到虚拟网络
用过Azure的同学都知道如何将虚拟机加入虚拟网络,也非常简单,但是对于将应用(WebSite)集成到虚拟网络中却很少听到.今天我要讲的就是如何直接将我们部署在WebSite中的应用加入到虚拟网络. ...
- 设计模式之七:模板方法模式(Template Method)
模板方法模式: 定义了一个算法的基本操作骨架,并将算法的一些步骤延迟到子类中来实现. 模板方法模式让子类在不更改算法结构的前提下能够又一次定义算法的一些步骤. Define the skeleton ...
- ChibiOS/RT 2.6.9 CAN Driver
Detailed Description Generic CAN Driver. This module implements a generic CAN (Controller Area Netwo ...
- 关于C++类型检查的一点小挫折
问题: 定义了一个float型数组Lut[],我让一个整型指针指向数组名int * Address=lut ; VS2008报错: error C2440: '=' : cannot conv ...
- Visio中如何绘制黑白图像
- [Winform]setupfactory制作安装包卸载输入密码进行验证
摘要 项目有这样一个需求,在体验机上安装了一个软件,如果有用户卸载的时候,给与输入密码验证的提示,当然强制删除软件所在目录除外.那么这个有办法实现吗? 解决办法 在卸载的时候,用户单击下一步的时候进行 ...