0
点赞
收藏
分享

微信扫一扫

60. Permutation 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.

思路:n 个数字有 n!种全排列,每种数字开头的全排列有 (n - 1)!(n!/ n)种。
所以用 k / (n - 1)! 就可以得到第 k 个全排列是以第几个数字开头的。
用 k % (n - 1)! 就可以得到第 k 个全排列是某个数字开头的全排列中的第几个。
这又变成了最初的问题设置。
对于以某个数字开头的全排列(第一个数字固定后的全排列,不再理会第一个数字),它有 (n - 1)! 种全排列,那么这些全排列中,每个数字开头的全排列有 (n - 2)! ((n-1)! / (n-1))。
依次类推。

在代码中为了使每次选中的数字不会重复。先用一个数组记录下所有数字(1 到 n),每次使用了某个数字后,就把该数字从数组中移除。
上述我们提到的“第 k 个全排列是以第几个数字开头“,这”第几个数字“并不是指该数字本身,而是数组中的”第几个数字“。

public class Solution {
public String getPermutation(int n, int k) {
ArrayList<Integer> list = new ArrayList();
StringBuilder sb = new StringBuilder();
int factorial = 1;
for(int i = 2; i <= n - 1; i ++)
factorial *= i;
for(int i = 1; i <= n; i ++)
list.add(i);
k --;
int round = n - 1;
while(round >= 0) {
int num = list.get(k / factorial);
sb.append(num);
list.remove(k / factorial);
if(round > 0) {
k = k % factorial;
factorial /= round;
}
round --;
}
return sb.toString();
}
}

class Solution {
public String getPermutation(int n, int k) {
StringBuilder res = new StringBuilder();
ArrayList<Integer> nums = new ArrayList<>();
for(int i = 1; i <= n; i++) {
nums.add(i);
}
k--;
int[] factorial = new int[n];
factorial[0] = 1;
for(int i = 1; i < n; i++) {
factorial[i] = factorial[i-1] * i;
}

while(n>0) {
int p = k / factorial[n-1];
res.append(nums.remove(p));
k = k % factorial[n-1];
n = n-1;
}
return res.toString();
}
}

class Solution {
public String getPermutation(int n, int k) {
List<Character> list = new LinkedList<>();
int product = 1;
for (int i = 1; i <= n; i++) {
if (i < n) product *= i;
list.add((char) (i + '0'));
}
StringBuilder sb = new StringBuilder();
while (n-- > 0) {
int i = k > 0 ? (k - 1) / product : 0;
sb.append(list.get(i));
list.remove(i);
k -= i * product;
if (n > 0) product /= n;
}
return sb.toString();
}
}


举报

相关推荐

0 条评论