{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "cd3b923120a146c78e344668ed62a1ae",
  "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": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Solution(object):\n\tdef getPermutation(self, n, k):\n\t\t\"\"\"\n\t\t:type n: int\n\t\t:type k: int\n\t\t:rtype: str\n\t\t\"\"\"\n\t\timport math\n\t\tres=[\"\"]\n\t\tdef generate(s,k):\n\t\t\tn=len(s)\t\n\t\t\tif n<=2:\n\t\t\t\tif k==2:\n\t\t\t\t\tres[0]+=s[::-1]\n\t\t\t\telse:\n\t\t\t\t\tres[0]+=s\n\t\t\t\treturn\n\t\t\tstep =  math.factorial(n-1) \n\t\t\tyu=k%step\n\t\t\tif yu==0:\n\t\t\t\tyu=step\n\t\t\t\tc=k//step-1 \n\t\t\telse:\n\t\t\t\tc=k//step\n\t\t\tres[0]+=s[c]\n\t\t\tgenerate(s[:c]+s[c+1:],yu)\n\t\t\treturn \n\t\ts=\"\"\n\t\tfor i in range(1,n+1):\n\t\t\ts+=str(i) \n\t\tgenerate(s,k)\n\t\treturn res[0]\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.getPermutation(3, 2))",
    "language": "python"
  },
  "node_id": "dailycode-c3fbc3654d0649a09f019aa4b33abf91",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600471016"
}