Skip to content

Commit 5d60e5f

Browse files
committed
60
1 parent 6c4c600 commit 5d60e5f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

60.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
string getPermutation(int n, int k) {
4+
string st(n,'0');
5+
int fact=1;
6+
for(int i=1;i<=n;i++){
7+
fact*=i;
8+
st[i-1]+=(i);
9+
}
10+
for(int i=0;i<n;i++){
11+
int block_num=fact/(n-i);
12+
fact/=(n-i);
13+
int first_index=i+(k-1)/(block_num);
14+
char c=st[first_index];
15+
k=k-(((k-1)/(block_num))*(block_num));
16+
for(int j=first_index;j>=i+1;j--){
17+
st[j]=st[j-1];
18+
}
19+
st[i]=c;
20+
cout<<st<<" "<<k<<" ";
21+
}
22+
return st;
23+
}
24+
};

0 commit comments

Comments
 (0)