-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathq060_PermutationSequence.java
More file actions
54 lines (48 loc) · 1.29 KB
/
q060_PermutationSequence.java
File metadata and controls
54 lines (48 loc) · 1.29 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
package leetcode_algorithm;
import java.util.LinkedList;
import java.util.List;
/**
* The set [1,2,3,¡,n] contains a total of n! unique permutations.
* <p>
* By listing and labeling all of the permutations in order,
* We get the following sequence (ie, for n = 3):
* <p>
* "123"
* "132"
* "213"
* "231"
* "312"
* "321"
* Given n and k, return the kth permutation sequence.
* <p>
* Note: Given n will be between 1 and 9 inclusive.
* <p>
* Subscribe to see which companies asked this question.
*/
public class q060_PermutationSequence {
public static void main(String[] args) {
System.out.println(new q060_PermutationSequence().getPermutation(3, 3));
}
/**
* ½â·¨1(¸öÈ˽ⷨ)
* @param n
* @param k
* @return
*/
public String getPermutation(int n, int k) {
List<Integer> num = new LinkedList<>();
for (int i = 1; i <= n; i++) num.add(i);
int[] fact = new int[n];
fact[0] = 1;
for (int i = 1; i < n; i++) fact[i] = i * fact[i - 1];
k--;
StringBuilder sb = new StringBuilder();
for (int i = n - 1; i >= 0; i--) {
int ind = k / fact[i];
k = k % fact[i];
sb.append(num.get(ind));
num.remove(ind);
}
return sb.toString();
}
}