Skip to main content

排列序列

题目

给出集合 [1, 2, 3, ..., n], 其所有元素共有 n! 种排列.

按大小顺序列出所有排列情况, 并一一标记, 如当 n = 3 时, 所有排列为: "123", "132", "213", "231", "312", "321"

给定 nk, 返回第 k 个排列.

提示:
  • 1 <= n <= 9
  • 1 <= k <= n!
示例
输入: (n = 3), (k = 3)

输出: '213'
输入: (n = 4), (k = 9)

输出: '2314'
输入: (n = 3), (k = 1)

输出: '123'

题解

初始想法是借鉴 46. 全排列, 获取第 k 个生成的 track, 然而 hard 毕竟是 hard, 果然就超时了...

/**
* @param {number} n
* @param {number} k
* @return {string}
*/
var getPermutation = function (n, k) {
let res = ''
let count = 0

const dfs = (track) => {
if (track.length === n) {
count += 1

if (count === k) {
res = track.join('')
return
}
}

for (let i = 1; i <= n; i++) {
if (!track.includes(i)) {
track.push(i)
dfs(track)
track.pop()
}
}
}

dfs([])

return res
}