-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPermutation_60.cpp
67 lines (56 loc) · 1.07 KB
/
getPermutation_60.cpp
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
64
65
66
67
#include<string>
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
bool dfs(string& tmp){
if(tmp.size() == n){
//cout << "tmp " << tmp << endl;
if(k == 1){
res = tmp;
return true;
}else{
k--;
}
}
for(int i = 0; i < n; ++i){
if(visited[i])
continue;
tmp.push_back('1' + i);
visited[i] = true;
if(dfs(tmp))
return true;
tmp.pop_back();
visited[i] = false;
}
return false;
}
int jiecheng(int x){
if(x == 0 || x == 1){
return 1;
}
return x * jiecheng(x-1);
}
string getPermutation(int n, int k) {
// 从 i 开始的排序数有 m 个, k/m 即为只需遍历从 k/m 开始的排序
int m = jiecheng(n-1);
int start = k/m;
this->n = n;
this->k = k % (m+1);
visited.resize(n, false);
visited[start] = true;
string tmp;
tmp.push_back('1' + start);
dfs(tmp);
return res;
}
private:
vector<bool> visited;
int n;
int k;
string res;
};
int main(){
return 0;
}