# Middle-题目93：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.

Step1.首先初始化动态数组list=[1,2,3,4],4个数的全排列有4!=24种，分为4组，则15在第2组种，组内序号为3（组号和组内序号都从0开始）则取出list的2号元素’3’加入答案中，并更新n=3,k=3.
Step2.list=[1,2,4],3个数的全排列有6种，分为3组，则3在第1组，组内序号为1.取出list的1号元素’2’，并更新n=2,k=1
Step3.list=[1,4]，2个数的全排列有2种，分为2组，则1在第1组，组内序号为0，取出list的1号元素’4’，并更新n=1,k=0
Step4.list=[1],1个数的排列只有1种，加入第0号元素’1’即可。

public class Solution {
private String current = "";
public String getPermutation(int n, int k) {
List<Integer> list = new ArrayList<Integer>();
for(int i = 1;i<=n;i++)
backtrack(list, n, k);
return current;
}
private void backtrack(List<Integer> list, int n, int k) {
if(list.isEmpty())
return;
else {
int groups = factorial(n-1);
int groupid = (k-1)/groups;
current+=String.valueOf(list.get(groupid));
list.remove(groupid);
backtrack(list, n-1, k-groupid*groups);
}
}
public int factorial(int num){
int sum=1;
for(int i=1;i<=num;i++){
sum *= i;
}
return sum;
}
}

4ms,beats 5.39%,众数3ms,41.65%
Cmershen的碎碎念：

#### 60. Permutation Sequence | Java最短代码实现

2016-03-24 15:28:43

#### (Java)LeetCode-60. Permutation Sequence

2016-10-05 23:52:58

#### 60. Permutation Sequence（难）

2016-08-25 21:30:18

#### Leetcode #60. Permutation Sequence 排列组合序列 解题报告

2016-04-30 23:06:32

#### LeetCode 60. Permutation Sequence（排列序列）

2016-05-21 09:05:38

#### [C++]LeetCode: 114 Permutation Sequence（返回第k个阶乘序列——寻找数学规律）

2015-01-20 20:48:30

#### LeetCode(60): Permutation Sequence (C++)

2015-12-01 10:44:16

#### LeetCode 60 Permutation Sequence （Python实现及详解）

2017-08-01 16:23:00

#### 60. permutation sequence leetcode python

2015-03-27 01:16:52

#### LeetCode-60-Permutation Sequence(找规律)-Medium

2016-03-04 09:07:30