解题的窍门是,每一位数字可以用,K对该数字后面所有数字的组合数量相除取整确定。
class Solution:
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
jc=[]
jie=1
for i in range(1,n+1):
jie*=i
jc.append(jie)
s=''
digit=['1','2','3','4','5','6','7','8','9']
k=k-1
for n in range(n-1,0,-1):
zheng = k//jc[n-1]
k = k%jc[n-1]
s+=digit[zheng]
digit.remove(digit[zheng])
s+=digit[0]
return s