classSolution{publicStringgetPermutation(int n,int k){List<Integer> list =newArrayList<>();for(int i =1; i <= n; i++){
list.add(i);}returndfs(n, k, list,newStringBuilder());}publicStringdfs(int n,int k,List<Integer> list,StringBuilder sb){if(k ==1){for(int i =0; i < list.size(); i++){
sb.append(list.get(i));}return sb.toString();}// 选择某个节点后剩余节点的叶子数量(阶乘)int sum =calculateFactorial(list);for(int i =0; i < list.size(); i++){if(k <= sum){Integer num = list.get(i);
sb.append(num);
list.remove(num);break;}// 选择当前数字的叶子节点数量少于k,一定不在当前数字下的叶子节点中
k -= sum;}returndfs(n, k, list, sb);}publicintcalculateFactorial(List<Integer> list){int sum =1;int x = list.size()-1;while(x >1){
sum *= x;
x--;}return sum;}}