Share my 6 line clean solution


  • 0
    X
    def getPermutation(self, n, k):
        ret, ns, k = "", range(1, n + 1), k - 1
        while len(ns):
            f = math.factorial(len(ns) - 1)
            x, r = k / f, k % f
            k = r
            ret += str(ns.pop(x))
        return ret
    

    I think it's clean, straight forward implementation.

    I said 6 lines, but I did combine several "=" together :)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.