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 k th permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
import java.util.ArrayList;
import java.util.List;
public class Solution {
public String getPermutation(int n, int k) {
int [] jc = new int[n]; //阶乘
List<Integer> list = new ArrayList<>();
jc[0] = 1;
for(int i=1;i<n;i++){
list.add(i);
jc[i] = jc[i-1]*i;
}
list.add(n);
StringBuilder sb = new StringBuilder();
int arg1;
k = k-1;
for(int i=0;i<n;i++){
arg1 = k/jc[n-1-i];
k = k- arg1*jc[n-1-i];
sb.append(list.get(arg1));
list.remove(arg1);
}
return sb.toString();
}
}