52ms recursive Python solution


  • 0
    H
    class Solution(object):
        def getPermutation(self, n, k, nums = [1]):
            """
            :type n: int
            :type k: int
            :rtype: str
            """
            if n == 1: 
                return str(nums[0])
            else:  
                if nums == [1]: 
                    nums = range(1, n + 1)
                pos = (k - 1) / math.factorial(n - 1)
                head = str(nums.pop(pos))
                tail = self.getPermutation(n - 1, k % math.factorial(n - 1), nums)
                return head + tail
    

Log in to reply
 

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