getPermutation_60.py 文件源码

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

项目:leetcode 作者: Firkraag 项目源码 文件源码
def getPermutation(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: str
        """
        if n <= 0 or k <= 0:
            return ""
        from math import factorial, ceil
        solution = []
        digits = [str(i) for i in range(0, n + 1)]
        while n > 0:
            n2 = factorial(n - 1)
            index = int(ceil(1.0 * k / n2))
            solution.append(digits.pop(index))
            n -= 1
            k -= n2 * (index - 1)
        return ''.join(solution)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号