-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPermutation Sequence.java
More file actions
53 lines (46 loc) · 1.29 KB
/
Permutation Sequence.java
File metadata and controls
53 lines (46 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
/*
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
*/
public class Solution {
public String getPermutation(int n, int k) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ArrayList<Integer> nums = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
nums.add(i);
}
int[] index = new int[n];
int i = 0;
//IMPORTANT: there are k - 1 permutations before
k = k - 1;
while (k > 0) {
index[i] = k / getFact(n - 1);
k = k % getFact(n - 1);
n--;
i++;
}
String result = "";
for (int j = 0; j < index.length; j++) {
result += nums.get(index[j]);
nums.remove(index[j]);
}
return result;
}
public int getFact(int x) {
int result = 1;
for (int i = x; i > 1; i--) {
result *= i;
}
return result;
}
}