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 ...
随机推荐
- webpack-manifest-plugin
[webpack-manifest-plugin] Webpack plugin for generating an asset manifest. This will generate a mani ...
- Spread Syntax
[Spread Syntax] The spread syntax allows an expression to be expanded in places where multiple argum ...
- Dubbo启动过程(Spring方式)详解
一.使用Spring xml配置方式的启动过程 1. 解析XML,注册Bean 2. 监听Spring事件 3. 启动或关闭dubbo 二.详细过程 1. 解析XML,注册Bean 利用Spring提 ...
- sqlmap自动注入
基于python2.7开发 git clone https://github.com/sqlmapproject/sqlmap.git sqlmap-dev
- centos 7 搭建openvpn-2.4.6
参考:https://blog.csdn.net/weixin_42250094/article/details/80384863 http://www.startupcto.com/server-t ...
- poj1850(组合数)
题目链接:http://poj.org/problem;jsessionid=B0D9A01EC0F1043088A37454B6CED469?id=1850 题意:给字符串编号,该字符串必须满足由小 ...
- oracle中获取当前整点和上一个小时整点,日期类型
select to_date(to_char(sysdate,'yyyy-mm-dd hh'),'yyyy-mm-dd hh:mi:ss') from dual;select to_date(to_c ...
- http://www.rabbitmq.com/documentation.html
http://www.rabbitmq.com/documentation.html https://www.gitbook.com/book/geewu/rabbitmq-quick/details
- 六.Spring与RabbitMQ集成--HelloWorld
spring对RabbitMQ做了很好的集成,我们称之为spring AMQP,其官方文档写得十分详尽,文档地址:https://docs.spring.io/spring-amqp/referenc ...
- wk1&2 字符串
[CQ] 自增怎么样都增了,赋值不一定: int x = 2; int y = 2; int i = ++x; int j = y++; System.out.println(x); System.o ...