递归生成思想。比如n=4,k=9。三位数的全排列为6,9//6=1,所以第一位数字是2。剩余134,k=3. 3//2=1 第二位数字是3。…
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]