-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathleetcode_0060_Permutation_Sequence.java
More file actions
63 lines (59 loc) · 1.89 KB
/
leetcode_0060_Permutation_Sequence.java
File metadata and controls
63 lines (59 loc) · 1.89 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
// AC: Runtime: 1 ms, faster than 98.32% of Java online submissions for Permutation Sequence.
// Memory Usage: 36 MB, less than 99.03% of Java online submissions for Permutation Sequence.
// get the forwarding bit of every position, then construct the string by the bit-list
// T:O(n^2), S:O(n)
//
class Solution {
public String getPermutation(int n, int k) {
k--;
// get the forwarding bit of every position on the base of [1,2,3,4,5,...]
List<Integer> bit = new LinkedList<>();
for (int i = n - 1; i >= 1; i--) {
int factorI = getFactor(i);
if (k >= factorI) {
int temp = k / factorI;
bit.add(temp);
k = k % factorI;
} else {
bit.add(0);
}
}
bit.add(0);
// forwarding by every bit
int[] used = new int[n + 1];
StringBuilder ret = new StringBuilder();
for (int i = 0; i < n; i++) {
if (bit.get(i) != 0) {
int count = 0;
for (int j = 1; j < used.length; j++) {
if (used[j] == 0) {
if (count < bit.get(i)) {
count++;
} else {
ret.append(j);
used[j] = 1;
break;
}
}
}
} else {
for (int j = 1; j < used.length; j++) {
if (used[j] == 0) {
ret.append(j);
used[j] = 1;
break;
}
}
}
}
return ret.toString();
}
private int getFactor(int n) {
int ret = 1;
while (n > 1) {
ret *= n;
n--;
}
return ret;
}
}