60. Permutation Sequence
2016-09-20 本文已影响0人
阿团相信梦想都能实现
import math
class Solution(object):
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
#the nth number rotate every (n-1)! times
res=''
num=[i for i in xrange(1,10)]
k=k-1
fact=math.factorial(n-1)
for i in reversed(xrange(n)):
out=num[k/fact]
res+=str(out)
num.remove(out)
if i>0:
k%=fact
fact/=i
return res