We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。
[1,2,3,…,n]
n!
按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:
n = 3
"123"
"132"
"213"
"231"
"312"
"321"
给定 n 和 k,返回第 k 个排列。
n
k
Input: n = 3, k = 3 Output: "213"
Input: n = 4, k = 9 Output: "2314"
The text was updated successfully, but these errors were encountered:
/** * @param {number} n * @param {number} k * @return {string} */ var getPermutation = function(n, k) { const counts = new Array(n); const digits = new Array(n); for (let i = 0; i < n; i++) { digits[i] = i + 1; counts[i] = (counts[i - 1] || 1) * digits[i]; } let result = ''; for (let i = 1; i <= n; i++) { const digitIndex = Math.ceil(k / counts[n - i - 1]) - 1; k %= counts[n - i - 1]; result += digits.splice(digitIndex, 1); } return result; };
function getPermutation(n: number, k: number): string { const counts: number[] = new Array(n); const digits: number[] = new Array(n); for (let i = 0; i < n; i++) { digits[i] = i + 1; counts[i] = (counts[i - 1] || 1) * digits[i]; } let result = ''; for (let i = 1; i <= n; i++) { const digitIndex = Math.ceil(k / counts[n - i - 1]) - 1; k %= counts[n - i - 1]; result += digits.splice(digitIndex, 1); } return result; };
Sorry, something went wrong.
No branches or pull requests
60. Permutation Sequence
给出集合
[1,2,3,…,n]
,其所有元素共有n!
种排列。按大小顺序列出所有排列情况,并一一标记,当
n = 3
时, 所有排列如下:"123"
"132"
"213"
"231"
"312"
"321"
给定
n
和k
,返回第k
个排列。Note
Example 1
Example 2
The text was updated successfully, but these errors were encountered: