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 ...
随机推荐
- ip地址库选择
目前市面上常用的ip地址库,有以下几种 1,新浪的api接口(限制未知)http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js& ...
- QT Unexpected CDB exit 问题的解决办法
行QT进行debug时,提示 Unexpected CDB exit ,The CBD process terminated.. QtCreator 默认是没有调试器的,因此需要用户额外安装. win ...
- php安全处理
1.php.ini 修改 open_basedir='d:\wwwroot' //配置只能访问指定的网站目录 2.php.ini 修改 disable_funcitons=system,passthr ...
- ConcurrentBag同线程元素的添加和删除
https://www.mgenware.com/blog/?p=232 ConcurrentBag<T>对于同一个线程值的添加和删除是非常快的,因为ConcurrentBag内部将数据按 ...
- spring in action第一章小结1
1 spring基本理念是简化java开发. 使用以下4个策略简化java开发 1) 基于POJO的轻量级和最小侵入性编程 2)通过使用DI和AOP实现松耦合 3)基于切面和惯例进行声明式编程 4)通 ...
- overflow知多少
本文地址: http://www.hicss.net/some-overflow-knowledge/ 最近在研究OOCSS,当打开template.css阅读第一行时,震惊了,第一眼居然没看懂... ...
- tensorboard 之 TF可视化
tensorboard是TF提供的一个可视化的工具 1.tensorboard可视化的数据来源? 将tensorflow程序运行过程中输出的日志文件进行可视化展示. 1.1 tensorflow怎样输 ...
- python学习之sys模块
查看python的版本 >>> sys.version_info[] sys.argv 列表对象,传入模块参数的都会放入列表中. #-*- coding: utf-8 -*- # i ...
- 安装好Oracle和PLSQLDeveloper后,PLSQLDeveloper登录时没有可选数据库和连接为问题
1.登录PL/SQL Developer 这里省略Oracle数据库和PL/SQL Developer的安装步骤,注意在安装PL/SQL Developer软件时,不要安装在Program Files ...
- ubuntu MySQL数据库输入中文乱码 解决方案
一.登录MySQL查看用SHOW VARIABLES LIKE ‘character%’;下字符集,显示如下:+--------------------------+----------------- ...