Skip to content

Commit b16c8f5

Browse files
authored
Update 60-permutation-sequence.js
1 parent 28b2ead commit b16c8f5

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

60-permutation-sequence.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,24 @@ const getPermutation = function (n, k) {
2828
* @return {string}
2929
*/
3030
const getPermutation = function(n, k) {
31-
let pos = 0
32-
const numbers = []
33-
const factorial = Array(n + 1).fill(0)
34-
let str = ''
35-
36-
let sum = 1
31+
const factorial = []
32+
const nums = []
33+
let res = ''
3734
factorial[0] = 1
38-
39-
for(let i = 1; i <= n; i++) {
35+
for(let i = 1, sum = 1; i <= n; i++) {
4036
sum *= i
4137
factorial[i] = sum
4238
}
4339
for(let i = 1; i <= n; i++) {
44-
numbers.push(i)
40+
nums.push(i)
4541
}
4642
k--
47-
for(let i = 1; i <= n; i++) {
43+
for(let i = 0; i <= n; i++) {
4844
const idx = ~~(k / factorial[n - i])
49-
str += numbers[idx]
50-
numbers.splice(idx, 1)
45+
res += nums[idx]
46+
nums.splice(idx, 1)
5147
k -= idx * factorial[n - i]
5248
}
5349

54-
return str
50+
return res
5551
};

0 commit comments

Comments
 (0)