9 line AC solution


  • 0
    F
        def getPermutation(self, n, k):
            def this_pos(cands, k, pre):
                if len(cands) == 1:
                    return pre + str(cands[0])
                fact = math.factorial(len(cands)-1)
                ind = (k-1) / fact
                this = cands[ind]
                return this_pos(cands[:ind] + cands[ind+1:], ((k-1) % fact) + 1, pre+str(this))
            return this_pos(range(1, n+1), k, "")
    

Log in to reply
 

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