Permutation Sequence LT60
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 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.
- Given k will be between 1 and n! inclusive.
Example 1:
Input: n = 3, k = 3
Output: "213"
Example 2:
Input: n = 4, k = 9
Output: "2314"
By observing the sequence for n = 3, we can see the permutation sequence is 1 + [2, 3] permutation, 2 + [1, 3] permutation, 3 + [1, 2] permutation. The sequence with first element = 1 has value k = 1, 2, with first element = 2 has value 3, 4, with first elemnt = 3 has value 5, 6, it's not hard to see that the index of the first element is (k-1)/2!
Next, we want to find the first element for n = 2, since there is only 2! permutation, the new k would be in the range [1, 2!], if you observe the original k and the permutation:
[2, 3] k = 0 -> new k = 0
[3, 2] k = 1 -> new k = 1
[1, 3] k = 2 -> new k = 0
[3, 1] k = 3 -> new k = 1
[1, 2] k = 4 -> new k = 0
[2, 1] k = 5 -> new k = 1
new k = old k / 2!, it's just the same problem, with smaller n and k, hence we can use either recursive or iterative to solve it.
Key taken: --k, which makes the caculation easier also rules easier to observe, if k == 0, it's the first sequence in the permutation, no further work needed.
Time complexity: O(n*2)
Iterative:
public class PermutationSequenceLT60 {
private int calculateFactorial(int n) {
int factorial = 1;
for(int i = 2; i <= n; ++i) {
factorial *= i;
}
return factorial;
}
public String permutation(int n, int k) {
StringBuilder result = new StringBuilder();
List<Integer> nums = new LinkedList<>();
for(int i = 1; i <= n; ++i) {
nums.add(i);
}
int factorial = calculateFactorial(n);
--k;
for(int i = n; k > 0 && i >= 1; --i) {
factorial = factorial/i;
int pos = k/factorial;
result.append(nums.remove(pos));
k = k%factorial;
}
for(int num: nums) {
result.append(num);
}
return result.toString();
}
public static void main(String[] args) {
PermutationSequenceLT60 p = new PermutationSequenceLT60();
System.out.println(p.permutation(4, 9));
for(int i = 1; i <= 6; ++i) {
System.out.println(p.permutation(3, i));
}
for(int i = 1; i<= 24; ++i) {
System.out.println(p.permutation(4, i));
}
}
}
Recursive:
public class PermutationSequenceLT60 {
private int calculateFactorial(int n) {
int factorial = 1;
for(int i = 2; i <= n; ++i) {
factorial *= i;
}
return factorial;
}
private void permutationHelper(int k, List<Integer> nums, StringBuilder result, int factorial) {
if(k == 0) {
for(int num: nums) {
result.append(num);
}
return;
}
int pos = k/factorial;
result.append(nums.remove(pos));
permutationHelper(k%factorial, nums, result, factorial/nums.size());
}
public String permutation(int n, int k) {
StringBuilder result = new StringBuilder();
List<Integer> nums = new LinkedList<>();
for(int i = 1; i <= n; ++i) {
nums.add(i);
}
int factorial = calculateFactorial(n-1);
permutationHelper(k-1, nums, result, factorial);
return result.toString();
}
public static void main(String[] args) {
PermutationSequenceLT60 p = new PermutationSequenceLT60();
System.out.println(p.permutation(4, 9));
for(int i = 1; i <= 6; ++i) {
System.out.println(p.permutation(3, i));
}
for(int i = 1; i<= 24; ++i) {
System.out.println(p.permutation(4, i));
}
}
}
Permutation Sequence LT60的更多相关文章
- 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 ...
- 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 (middle)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- [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总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
- 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 ...
随机推荐
- msf客户端渗透(九):获取PHP服务器shell
如果一个网页存在可以include外链的漏洞,我们可以利用这个漏洞include本机上的文件,从而获取web服务器的shell. 设置目标的IP 根据网页的路径设置参数 设置cookie 选择payl ...
- MONGO的简单语法,新手实用
window上启动方式:(简单略) mongod --dbpath E:\study_lib\mongodb\db --port=27000 show dababases; (创建配置文件的启动方式) ...
- JMeter学习(十一)WebSerivice测试计划(转载)
转载自 http://www.cnblogs.com/yangxia-test WebSerivice测试计划的取样器有两种方式:HTTP请求.SOAP/XML-RPC Request. 1. 测试计 ...
- 命令行执行jenkins,构建job(可传递参数)
背景| 组内做UI测试,需要每天晚上执行一遍jenkins任务,jenkins任务本身是参数化构建的.但是因为jenkins本身的定时执行没有办法指定特殊的参数,所以考虑使用命令行方式启动jenkin ...
- Android学习路-activity活动
activity即活动,是一种包含用户界面的组件,用于与用户进行交换 创建activity类 1.类继承Activity, activity传递一个bundle对象,可以获得onSaveInsta ...
- Appium 连手机失败Error: Android bootstrap socket crashed: Error: getaddrinfo ENOTFOUND localhost undefined:4724
问题:Appium执行,连接手机报下面的错误 Error: Android bootstrap socket crashed: Error: getaddrinfo ENOTFOUND localho ...
- 《深入理解java虚拟机》笔记
二.java内存区域与内存溢出异常 0.在内存管理领域,java与c/c++不同的是,在java虚拟机自动内存管理机制下,java不需要手动去为对象写配对的free内存的代码,不容易出现内存泄漏和内存 ...
- 第四章 栈与队列(a)栈接口与实现
- 使用ffmpeg解码 需要注意的内存泄漏问题
说说项目中遇到的两点失误: 1. AVFrame结构,av_frame_alloc申请内存,av_frame_free释放内存.容易混淆的是av_frame_unref,它的作用是释放音视频数据资源, ...
- 彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同
https://blog.csdn.net/qq_22522663/article/details/78729029 1. tf.Variable与tf.get_variabletensorflow提 ...