-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation Sequence.cpp
More file actions
37 lines (32 loc) · 914 Bytes
/
Copy pathPermutation Sequence.cpp
File metadata and controls
37 lines (32 loc) · 914 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
35
36
37
class Solution {
public:
string getPermutation(int n, int k) {
string s = "";
if(n == 0) return s;
vector<int> nums;
for(int i = 1; i<= n;i++) nums.push_back(i);
// calcualte n-1 factorial;
int * fn = new int[n];
fn[0] = 1;
for(int i = 0; i < n-1; i++){
fn[i] *= i+1;
}
k--;
for(int i = 0; i < n - 1; i++){
int j = i + k / fn[n - 2 - i];
if(i == j){
k = k % fn[n - 2 - i]; //wr
continue;
}
swap(nums[i], nums[j]);
int t = nums[j];
for(int m = j; m > i; m--)
nums[m] = nums[m - 1];
nums[i+1] = t;
k = k % fn[n - 2 - i];
}
for(int i = 0; i < n; i++)
s += '0'+ nums[i];
return s;
}
};