{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "94b082feff6a40fca932cef0969ef627",
  "keywords": "递归,数学",
  "title": "排列序列",
  "desc": [
    {
      "content": "\n<p>给出集合 <code>[1,2,3,...,n]</code>，其所有元素共有 <code>n!</code> 种排列。</p><p>按大小顺序列出所有排列情况，并一一标记，当 <code>n = 3</code> 时, 所有排列如下：</p><ol>\t<li><code>\"123\"</code></li>\t<li><code>\"132\"</code></li>\t<li><code>\"213\"</code></li>\t<li><code>\"231\"</code></li>\t<li><code>\"312\"</code></li>\t<li><code>\"321\"</code></li></ol><p>给定 <code>n</code> 和 <code>k</code>，返回第 <code>k</code> 个排列。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>n = 3, k = 3<strong><br />输出：</strong>\"213\"</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>n = 4, k = 9<strong><br />输出：</strong>\"2314\"</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>n = 3, k = 1<strong><br />输出：</strong>\"123\"</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>1 <= n <= 9</code></li>\t<li><code>1 <= k <= n!</code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic String getPermutation(int n, int k) {\n\t\tStringBuilder sb = new StringBuilder();\n\t\tList<Integer> candidates = new ArrayList<>();\n\t\tint[] factorials = new int[n + 1];\n\t\tfactorials[0] = 1;\n\t\tint fact = 1;\n\t\tfor (int i = 1; i <= n; ++i) {\n\t\t\tcandidates.add(i);\n\t\t\tfact *= i;\n\t\t\tfactorials[i] = fact;\n\t\t}\n\t\tk -= 1;\n\t\tfor (int i = n - 1; i >= 0; --i) {\n\t\t\tint index = k / factorials[i];\n\t\t\tsb.append(candidates.remove(index));\n\t\t\tk -= index * factorials[i];\n\t\t}\n\t\treturn sb.toString();\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-cb66e1d1a6c34674889f140de4b21ac1",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600471016"
}