Python solution in a dozen lines


  • 0
    J
    class Solution:
        # @return a string
        def getPermutation(self, n, k):
            dn=n-1
            dk=k
            arr=range(1,n+1)
            s=''
            while(dn>0):
                fct = math.factorial(dn)
                t=arr[(dk-1)/fct]
                dk=dk%fct
                s+=str(t)
                arr.remove(t)
                dn-=1
            s+=str(arr[0])
            return s

Log in to reply
 

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