Skip to content

Commit 12cea18

Browse files
authoredMay 13, 2024··
feat: add ts solution to lc problem: No.60 (#2801)
1 parent 6fab7d6 commit 12cea18

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
 

‎solution/0000-0099/0060.Permutation Sequence/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,31 @@ public class Solution {
225225
}
226226
```
227227

228+
```ts
229+
function getPermutation(n: number, k: number): string {
230+
let ans = '';
231+
const vis = Array.from({ length: n + 1 }, () => false);
232+
for (let i = 0; i < n; i++) {
233+
let fact = 1;
234+
for (let j = 1; j < n - i; j++) {
235+
fact *= j;
236+
}
237+
for (let j = 1; j <= n; j++) {
238+
if (!vis[j]) {
239+
if (k > fact) {
240+
k -= fact;
241+
} else {
242+
ans += j;
243+
vis[j] = true;
244+
break;
245+
}
246+
}
247+
}
248+
}
249+
return ans;
250+
}
251+
```
252+
228253
<!-- tabs:end -->
229254

230255
<!-- end -->

‎solution/0000-0099/0060.Permutation Sequence/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,31 @@ public class Solution {
210210
}
211211
```
212212

213+
```ts
214+
function getPermutation(n: number, k: number): string {
215+
let ans = '';
216+
const vis = Array.from({ length: n + 1 }, () => false);
217+
for (let i = 0; i < n; i++) {
218+
let fact = 1;
219+
for (let j = 1; j < n - i; j++) {
220+
fact *= j;
221+
}
222+
for (let j = 1; j <= n; j++) {
223+
if (!vis[j]) {
224+
if (k > fact) {
225+
k -= fact;
226+
} else {
227+
ans += j;
228+
vis[j] = true;
229+
break;
230+
}
231+
}
232+
}
233+
}
234+
return ans;
235+
}
236+
```
237+
213238
<!-- tabs:end -->
214239

215240
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function getPermutation(n: number, k: number): string {
2+
let ans = '';
3+
const vis = Array.from({ length: n + 1 }, () => false);
4+
for (let i = 0; i < n; i++) {
5+
let fact = 1;
6+
for (let j = 1; j < n - i; j++) {
7+
fact *= j;
8+
}
9+
for (let j = 1; j <= n; j++) {
10+
if (!vis[j]) {
11+
if (k > fact) {
12+
k -= fact;
13+
} else {
14+
ans += j;
15+
vis[j] = true;
16+
break;
17+
}
18+
}
19+
}
20+
}
21+
return ans;
22+
}

0 commit comments

Comments
 (0)
Please sign in to comment.