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 ...
随机推荐
- metasploit framework(七):密码嗅探
run 当嗅探到流量中的用户密码信息时打印出来,目前只支持FTP,http get , pop3 还可以对抓包文件,进行密码提取,设置需要提取的文件路径 run就能提取里面的用户密码信息 查看和停掉某 ...
- jquery+jquery.pagination+php+ajax 无刷新分页
<!DOCTYPE html> <html ><head><meta http-equiv="Content-Type" content= ...
- 六:python 对象类型详解二:字符串(下)
一:字符串方法: 方法就是与特定对象相关联在一起的函数.从技术的角度来讲,它们是附属于对象的属性,而这些属性不过是些可调用的函数罢了.Python 首先读取对象方法,然后调用它,传递参数.如果一个方法 ...
- Maven 添加jar包到本地仓库
一.使用Maven命令安装jar包 前提:在windows操作系统中配置好了Maven的环境变量,怎么配置请自己百度,这里不介绍,可参考https://jingyan.baidu.com/articl ...
- Unity2017五子棋大战_人机_双人_UNET联网
五子棋大战源码工程基于Unity2017.2进行开发,分为人机.双人.UNET网络三种对战方式,配有案例讲解视频, 其中人机五子棋AI有三种开发难度,欢迎有兴趣的同学加入学习! . 目录 000-展示 ...
- Anaconda常用命令大全
使用conda 首先我们将要确认你已经安装好了conda 配置环境 下一步我们将通过创建几个环境来展示conda的环境管理功能.使你更加轻松的了解关于环境的一切.我们将学习如何确认你在哪个环境中,以及 ...
- 《centos系列》ubuntu终端链接centos服务器
首先你得知道centos的账户密码:如果你不知道可以直接在centos下使用root用户: passwd 用户名 直接更新用户的密码. 然后在ubuntu终端(前提是已经安装了ssh): ssh ad ...
- 【分布式架构】“spring cloud”与“dubbo”微服务的对比
秉承站在巨人的肩膀上,同时我也不想重复制造轮子,我发现了一系列关于“分布式架构”方面,我需要,同时能够解决我的一些疑惑.问题的博客,转载过来,原文链接: http://blog.csdn.net/ ...
- day 03
1.数字类型 int 数字主要是用于计算用的,使用方法并不是很多,就记住一种就可以: bit_length() 当前十进制用二进制表示时,最少使用的位数 s = 5 print(s.bit_leng ...
- 使用python语言计算n的阶乘
计算“1x2x3x4” def factorial(n): result = n ,n): result *= i return resultdef main(): print factorial(4 ...