# 排列序列

<p>给出集合 <code>[1,2,3,...,n]</code>，其所有元素共有 <code>n!</code> 种排列。</p><p>按大小顺序列出所有排列情况，并一一标记，当 <code>n = 3</code> 时, 所有排列如下：</p><ol>	<li><code>"123"</code></li>	<li><code>"132"</code></li>	<li><code>"213"</code></li>	<li><code>"231"</code></li>	<li><code>"312"</code></li>	<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>	<li><code>1 <= n <= 9</code></li>	<li><code>1 <= k <= n!</code></li></ul>

以下程序实现了这一功能，请你填补空白处内容：

```python
class Solution(object):
    def getPermutation(self, n, k):
        """
		:type n: int
		:type k: int
		:rtype: str
		"""
        import math
        res = [""]

        def generate(s, k):
            n = len(s)
            if n <= 2:
                if k == 2:
                    res[0] += s[::-1]
                else:
                    res[0] += s
                return
            step = math.factorial(n - 1)
            yu = k % step
            if yu == 0:
                yu = step
                c = k // step - 1
            else:
                c = k // step
            res[0] += s[c]
            ____________________;
            return

        s = ""
        for i in range(1, n + 1):
            s += str(i)
        generate(s, k)
        return res[0]


if __name__ == '__main__':
    s = Solution()
    print(s.getPermutation(3, 2))
```

## template

```python
class Solution(object):
    def getPermutation(self, n, k):
        """
		:type n: int
		:type k: int
		:rtype: str
		"""
        import math
        res = [""]

        def generate(s, k):
            n = len(s)
            if n <= 2:
                if k == 2:
                    res[0] += s[::-1]
                else:
                    res[0] += s
                return
            step = math.factorial(n - 1)
            yu = k % step
            if yu == 0:
                yu = step
                c = k // step - 1
            else:
                c = k // step
            res[0] += s[c]
            generate(s[:c] + s[c + 1:], yu)
            return

        s = ""
        for i in range(1, n + 1):
            s += str(i)
        generate(s, k)
        return res[0]


if __name__ == '__main__':
    s = Solution()
    print(s.getPermutation(3, 2))

```

## 答案

```python
generate(s[:c] + s[c + 1:], yu)
```

## 选项

### A

```python
generate(s[:c - 1] + s[c + 1:], yu)
```

### B

```python
generate(s[:c + 1] + s[c - 1:], yu)
```

### C

```python
generate(s[:c] + s[c:], yu)
```