diff --git a/solution/0000-0099/0060.Permutation Sequence/README.md b/solution/0000-0099/0060.Permutation Sequence/README.md index 2cde7391511b1..fb60e9b24865a 100644 --- a/solution/0000-0099/0060.Permutation Sequence/README.md +++ b/solution/0000-0099/0060.Permutation Sequence/README.md @@ -225,6 +225,31 @@ public class Solution { } ``` +```ts +function getPermutation(n: number, k: number): string { + let ans = ''; + const vis = Array.from({ length: n + 1 }, () => false); + for (let i = 0; i < n; i++) { + let fact = 1; + for (let j = 1; j < n - i; j++) { + fact *= j; + } + for (let j = 1; j <= n; j++) { + if (!vis[j]) { + if (k > fact) { + k -= fact; + } else { + ans += j; + vis[j] = true; + break; + } + } + } + } + return ans; +} +``` + diff --git a/solution/0000-0099/0060.Permutation Sequence/README_EN.md b/solution/0000-0099/0060.Permutation Sequence/README_EN.md index f24e140d671e1..b7faf21895925 100644 --- a/solution/0000-0099/0060.Permutation Sequence/README_EN.md +++ b/solution/0000-0099/0060.Permutation Sequence/README_EN.md @@ -210,6 +210,31 @@ public class Solution { } ``` +```ts +function getPermutation(n: number, k: number): string { + let ans = ''; + const vis = Array.from({ length: n + 1 }, () => false); + for (let i = 0; i < n; i++) { + let fact = 1; + for (let j = 1; j < n - i; j++) { + fact *= j; + } + for (let j = 1; j <= n; j++) { + if (!vis[j]) { + if (k > fact) { + k -= fact; + } else { + ans += j; + vis[j] = true; + break; + } + } + } + } + return ans; +} +``` + diff --git a/solution/0000-0099/0060.Permutation Sequence/Solution.ts b/solution/0000-0099/0060.Permutation Sequence/Solution.ts new file mode 100644 index 0000000000000..884f10399dc40 --- /dev/null +++ b/solution/0000-0099/0060.Permutation Sequence/Solution.ts @@ -0,0 +1,22 @@ +function getPermutation(n: number, k: number): string { + let ans = ''; + const vis = Array.from({ length: n + 1 }, () => false); + for (let i = 0; i < n; i++) { + let fact = 1; + for (let j = 1; j < n - i; j++) { + fact *= j; + } + for (let j = 1; j <= n; j++) { + if (!vis[j]) { + if (k > fact) { + k -= fact; + } else { + ans += j; + vis[j] = true; + break; + } + } + } + } + return ans; +}