-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#49.cc
More file actions
31 lines (27 loc) · 777 Bytes
/
LeetCode#49.cc
File metadata and controls
31 lines (27 loc) · 777 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
class Solution {
public:
string getPermutation(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> pi(n+1,0);
pi[1]=pi[0]=1;
for(int i=2;i<=n;i++) pi[i]=i*pi[i-1];
int pre = 0;
string ret = "";
vector<int> col(n,0);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
if(col[j]==0){
if(pre+pi[n-1-i]>=k){
col[j]=1;
ret+='1'+j;
break;
}
else{
pre+=pi[n-1-i];
}
}
}
return ret;
}
};