-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation Sequence.cpp
More file actions
34 lines (33 loc) · 877 Bytes
/
Permutation Sequence.cpp
File metadata and controls
34 lines (33 loc) · 877 Bytes
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
class Solution {
public:
/**
* @param n: n
* @param k: the kth permutation
* @return: return the k-th permutation
*/
string getPermutation(int n, int k) {
int base = 1, b = n-1;
for(int i = 2; i < n; i++) base *= i;
string ans;
bool visited[n];
k--;
memset(visited, 0, sizeof(visited));
for(int i = 0; i < n; i++) {
int j = k/base, a = 0;
k %= base;
if(i != n-1)
base /= (n-1-i);
for(int l = 0;l < n; l++) {
if(!visited[l]) {
a++;
if(a == j+1) {
visited[l] = true;
ans.push_back((l+'1'));
break;
}
}
}
}
return ans;
}
};