美文网首页
60. Permutation Sequence

60. Permutation Sequence

作者: 阿团相信梦想都能实现 | 来源:发表于2016-09-20 07:15 被阅读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
            
            

相关文章

网友评论

      本文标题:60. Permutation Sequence

      本文链接:https://www.haomeiwen.com/subject/wbonettx.html