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):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "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的更多相关文章

  1. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. [leetcode]Permutation Sequence @ Python

    原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...

  3. LeetCode: Permutation Sequence 解题报告

    Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...

  4. LeetCode——Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode]444. Sequence Reconstruction

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  6. [Leetcode] Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. LeetCode OJ--Permutation Sequence *

    求第k个排列. 刚开始按照一个排列一个排列的求,超时. 于是演算了一下,发下有数学规律,其实就是康托解码. 康托展开:全排列到一个自然数的双射 X=an*(n-1)!+an-1*(n-2)!+...+ ...

  8. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  10. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

随机推荐

  1. 【Linux设备驱动程序】Chapter 2 - 构造和运行模块

    Hello World 模块 #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Du ...

  2. poj_2773

    题目的愿意非常easy.给你一个n,求在升序排列的情况下,第k个与之相互素的数. 解法:首先我们要知道gcd(b×t+a,b)=gcd(a.b),那么接下来就非常easy了.全部与之互素的数都是以ph ...

  3. Netty4具体解释三:Netty架构设计

         读完这一章,我们基本上能够了解到Netty全部重要的组件,对Netty有一个全面的认识.这对下一步深入学习Netty是十分重要的,而学完这一章.我们事实上已经能够用Netty解决一些常规的问 ...

  4. setTime

    var getTime = function() { var _ = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09'], //补 ...

  5. 当 ftp 遇上 http Proxy

    在asp.net 开发中,有时需要使用到ftp 上传文件, 如果客户电脑使用http proxy 上网, 那么,客户电脑在使用ftp上传文件时,可能会出现以下错误: 使用 HTTP Proxy 時,不 ...

  6. atitit. 分销系统规划p8k

    atitit. 分销系统规划p8k 1. 商户平台管理 overview2 1.1. 分销业务管理2 1.2. 文案管理2 1.3. 订单管理3 1.4. 统计报表3 1.5. 财务结算3 1.6.  ...

  7. Windows/OS X下制作Mac安装U盘

    Windows下制作: 方法一:(适用于OSX 10.9以前) 前期准备:一台windows电脑 UltraISO软件 Mac系统镜像dmg(这里使用Mac os x 10.8.4) 至少8GB的U盘 ...

  8. ImageTag小案例

    其实不使用ImageIO,就是用一般的BufferedOutputStream+byte[] buffer也可以 关键在于通过response设置页面的MIME Type,自行Google~~~ 源代 ...

  9. python 脚本撞库国内“某榴”账号

    其实日常生活中我们的用户名和密码就那么几个,所以这给撞库带来了可能,本文主要给出python脚本撞库的一点粗浅代码.这里只讨论技术本生,代码中某榴的地址也已经改掉,避免被管理员误解禁言等发生,谢谢大家 ...

  10. 使用code::blocks搭建objective-c的IDE开发环境 支持 @interface

    网上有许多的关于 <使用code::blocks搭建objective-c的IDE开发环境>的文章. 大多是写了一个Helloworld 就结束了,今天试了试 添加了一个 @interfa ...