060_permutation_sequence.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:Machine_Learning_Playground 作者: yao23 项目源码 文件源码
def getPermutation(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: str
        """
        numbers = range(1, n+1)
        permutation = ''
        k -= 1
        while n > 0:
            n -= 1
            # get the index of current digit
            index, k = divmod(k, math.factorial(n))
            permutation += str(numbers[index])
            # remove handled number
            numbers.remove(numbers[index])

        return permutation
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号