LeetCode 60. Permutation Sequence

题目

The set [1,2,3,...,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order, we get the following sequence for n = 3:

1
2
3
4
5
6
1. "123"
2. "132"
3. "213"
4. "231"
5. "312"
6. "321"

Given n and k, return the k^th permutation sequence.

Note:

  • Given n will be between 1 and 9 inclusive.
  • Given k will be between 1 and n! inclusive.

Example 1:

1
2
Input: n = 3, k = 3
Output: "213"

Example 2:

1
2
Input: n = 4, k = 9
Output: "2314"

思路

permutations.

代码

1
2
3
4
5
6
7
8
9
class Solution(object):
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
x={1:'1',2:'12',3:'123',4:'1234',5:'12345',6:'123456',7:'1234567',8:'12345678',9:'123456789'}
return ["".join(i) for i in permutations(x[n])][k-1]