60. 排列序列
这道题虽然是困难的,但它本质上就是个找规律题,考察程序员基本功
看了一下别的题解,感觉写的都比我复杂,就不往上放了
代码:
class Solution {
public String getPermutation(int n, int k) {
int[] record = new int[n];
k=k-1;
Arrays.fill(record, 1);
String result = "";
int length = record.length;
while (n > 0) {
int jieCeng = getJieCeng(n - 1);
int i = k / jieCeng, f = -1, j = 0;
for (; j < length; j++) {
if (record[j] == 1) f++;
if (f == i) break;
}
record[j] = 0;
result += j+1;
n--;
k %= jieCeng;
}
return result;
}
public int getJieCeng(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}