-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode_00060.java
More file actions
65 lines (61 loc) · 1.59 KB
/
LeetCode_00060.java
File metadata and controls
65 lines (61 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.github.jerring.leetcode;
import java.util.ArrayList;
import java.util.List;
public class LeetCode_00060 {
// public String getPermutation(int n, int k) {
// char[] cs = new char[n];
// for (int i = 0; i < n; ++i) {
// cs[i] = (char) ('1' + i);
// }
// while (--k > 0) {
// nextPermutation(cs, n);
// }
// return String.valueOf(cs);
// }
//
// private void nextPermutation(char[] cs, int n) {
// int i = n - 2;
// while (i >= 0 && cs[i] >= cs[i + 1]) {
// --i;
// }
// if (i >= 0) {
// int j = n - 1;
// while (cs[i] >= cs[j]) {
// --j;
// }
// swap(cs, i, j);
// reverse(cs, i + 1, n - 1);
// }
// }
//
// private void reverse(char[] cs, int i, int j) {
// while (i < j) {
// swap(cs, i++, j--);
// }
// }
//
// private void swap(char[] cs, int i, int j) {
// char tmp = cs[i];
// cs[i] = cs[j];
// cs[j] = tmp;
// }
public String getPermutation(int n, int k) {
List<Integer> list = new ArrayList<>(n);
for (int i = 1; i <= n; ++i) {
list.add(i);
}
int[] f = new int[n];
f[0] = 1;
for (int i = 1; i < n; ++i) {
f[i] = f[i - 1] * i;
}
--k;
StringBuilder sb = new StringBuilder(n);
for (int i = n - 1; i >= 0; --i) {
int j = k / f[i];
k %= f[i];
sb.append(list.remove(j));
}
return sb.toString();
}
}