Elegant simple solution in python


  • 0
    O
    class Solution(object):
        def getPermutation(self, n, k):
            """
            :type n: int
            :type k: int
            :rtype: str
            """
            ans = ""
            rec = set()
            for i in xrange(1, n + 1): rec.add(i)
            pos = n - 1
            while rec:
                cur = 0
                for i in rec:
                    if k - math.factorial(pos) > 0:
                        k -= math.factorial(pos)
                    else:
                        ans += str(i)
                        rec.remove(i)
                        pos -= 1
                        break
            return ans

Log in to reply
 

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