Leetcode-Permuation Sequence
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.
Newest Solution, a much shorter one:
public class Solution {
public String getPermutation(int n, int k) {
boolean[] used = new boolean[n]; int index = n;
int total = 1;
for (int i=1;i<=n;i++){
total *= i;
}
StringBuilder builder = new StringBuilder();
while (index!=0){
total = total / index;
int count = (k-1) / total + 1;
k = (k-1) % total + 1;
int ind = 0;
for (int i=0;i<n;i++)
if (!used[i]){
ind++;
if (ind==count){
used[i] = true;
builder.append(i+1);
break;
}
}
index--;
}
return builder.toString();
}
}
Solution 1:
We use recursive method to get the sequence one by one. However, this method is slow.
public class Solution {
public String getPermutation(int n, int k) {
int[] seq = new int[n+1];
int level = 1;
boolean[] used = new boolean[n+1];
Arrays.fill(used,false);
Arrays.fill(seq,0);
used[0] = true;
int count = 0;
String res = "";
while (true){
if (level==n){
count++;
if (count==k){
for (int i=1;i<=n;i++)
if (!used[i]){
seq[level] = i;
break;
}
for (int i=1;i<=n;i++)
res += Integer.toString(seq[i]);
break;
} else {
level--;
continue;
}
} int val = seq[level];
//NOTE: we need the first condition, because used array does not have n+1.
while (val<n+1 && used[val])
val++;
if (val==n+1){
if (seq[level]!=0) used[seq[level]] = false;
seq[level]=0;
level--;
continue;
} else {
if (seq[level]!=0) used[seq[level]] = false;
seq[level] = val;
used[val]=true;
level++;
}
} return res; }
}
Solution 2:
We actually can calculate the sequence. For sequences with n numbers, it is composed by n segments of sequences with n-1 numbers. The number of (n-1) sequences in each segment is (n-1)!. So if we are looking for kth n sequence, it is in (k/(n-1)!)th or (k/(n-1)!+1)th segment (boundary case considerred) which is means the number in the first place should be the (k/(n-1)!)th available number between 1 and n. The number of sequences we should count in this segment to find the target is (k%(n-1)!)th sequence in this segement. With this recurrence formula, we can directly calculate the string one place by one place.
NOTE: We need to consider the boundary cases where k%(n-1)!==0, in this case, it is the (n-1)!th sequence in the k/(n-1)! segment.
public class Solution {
public String getPermutation(int n, int k) {
int[] seq = new int[n+1];
boolean[] used = new boolean[n+1];
Arrays.fill(used,false);
Arrays.fill(seq,0);
String res = "";
int[] val = new int[n+1];
val[0] = 0;
val[1] = 1;
for (int i=2;i<=n;i++)
val[i] = val[i-1]*i; int left = k;
int num = n;
for (int i=1;i<n;i++){
int interval = val[num-1];
int step = left/interval;
int nextLeft = left%interval;
if (nextLeft==0)
nextLeft = interval;
else step++;
int index=0;
for (int j=1;j<=n;j++)
if (!used[j]){
index++;
if (index==step){
seq[i]=j;
used[j] = true;
break;
}
}
left = nextLeft;
num--;
}
for (int i=1;i<=n;i++)
if (!used[i]){
seq[n]=i;
break;
} for (int i=1;i<=n;i++)
res += Integer.toString(seq[i]);
return res; }
}
Leetcode-Permuation Sequence的更多相关文章
- [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]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
- LeetCode: Permutation Sequence 解题报告
Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...
- 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]444. Sequence Reconstruction
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [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 OJ--Permutation Sequence *
求第k个排列. 刚开始按照一个排列一个排列的求,超时. 于是演算了一下,发下有数学规律,其实就是康托解码. 康托展开:全排列到一个自然数的双射 X=an*(n-1)!+an-1*(n-2)!+...+ ...
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
随机推荐
- 转: RabbitMQ实现中AMQP与MQTT消息收发异同
转自:http://www.cnblogs.com/lucifer1997/p/9438186.html 实现了AMQP与MQTT(至多一次)后,用多个队列以topic exchange的方式用相同交 ...
- python-嵌套循环(Nested loop)-久久乘法表
嵌套-久久乘法 for i in range(1,10): for j in range(1,10): print('{} × {} = {}'.format(i,j,i*j))最外层的循环依次将数值 ...
- 当 ftp 遇上 http Proxy
在asp.net 开发中,有时需要使用到ftp 上传文件, 如果客户电脑使用http proxy 上网, 那么,客户电脑在使用ftp上传文件时,可能会出现以下错误: 使用 HTTP Proxy 時,不 ...
- ubuntu设置自动休眠
ubuntu16.04默认是永不休眠的,有时候忘了关机,那就惨了,一会用到没电为止. 设置方法: 进入“系统设置”->"安全与隐私"->"电源设置" ...
- NGUI ScrollView中的Bounds
获取到的Bounds值是固定的,是因为Bounds区域的计算是被动计算,需要主动调用使其刷新 scrollView.InvalidateBounds(); 另外Bounds的Min和Max似乎和NGU ...
- WEB前端面试题 分别使用2个、3个、5个DIV画出一个大的红十字
<!DOCTYPE html> <!--两个DIV--> <html> <body> <div style="width:100%;he ...
- CSS学习笔记(1)--浮动
总结:浮动只能在脱离文档流的当前位置向上浮动,不能像定位一样到处乱跑. 清除浮动,设置一个类.clear{clear:both;} 1.没有浮动,都独占一行: <!DOCTYPE html> ...
- (译)Getting Started——1.3.4 Writing a Custom Class(编写自定义的类)
在开发IOS应用中,当你编写自定义的类时,你会发现很多的特殊场合.当你需要把自定义的行为和数据包装在一起时,自定义的类非常有用.在自定义的类中,你可以定义自己的存储.处理和显示数据的方法. 例如,I ...
- Github上Fork代码,及源码修改
iOS开发中经常遇到这种情况,你使用的第三方库不能完全满足自己项目需要,只能修改源码来解决. 我们以前的解决办法是,添加到项目中直接修改源码.这样就有一个问题,不能和源库同步,当作者更新后你不能(po ...
- FastDFS 常见问题
FastDFS 常见问题 Q:/fdfs_trackerd: error while loading shared libraries: libevent-1.4.so.2: cannot open ...