0
点赞
收藏
分享

微信扫一扫

Redis远程字典服务器(5) —— list类型详解

Ad大成 2024-08-16 阅读 4

60. Permutation Sequence

class Solution:
    def getPermutation(self, n: int, k: int) -> str:
        def rec(k, l, ans, n):
            if(n==0): return
            # 保留第一个位置,剩下数字的组合
            leftCom = math.factorial(n - 1) #用于计算 (n-1) 的阶乘值
            ele = k // leftCom
            mod = k % leftCom
            ans += [l[ele]]
            l.pop(ele)
            rec(mod, l, ans, n - 1)
            return ''.join(map(str, ans)) # 用于将列表 ans 中的所有元素转换为字符串,并将这些字符串连接成一个完整的字符串
        l = [i for i in range(1, n+1)] # is a array contains 1 to n
        return rec(k - 1, l, [], n)
举报

相关推荐

0 条评论