@@ -0,0 +1,1404 @@
+%60%60%60%0AThe set %5B1,2,3,%E2%80%A6,n%5D contains a total of n! unique permutations.%0A%0ABy listing and labeling all of the permutations in order,%0AWe get the following sequence (ie, for n = 3):%0A%0A%22123%22%0A%22132%22%0A%22213%22%0A%22231%22%0A%22312%22%0A%22321%22%0AGiven n and k, return the kth permutation sequence.%0A%0ANote: Given n will be between 1 and 9 inclusive.%0A%60%60%60%0A%0Aclass Solution:%0A    # @param %7Binteger%7D n%0A    # @param %7Binteger%7D k%0A    # @return %7Bstring%7D%0A    def getPermutation(self, n, k):%0A        result = %5B%5D%0A        ilist = %5B%5D%0A        factor = 1%0A        %0A        for i in xrange(1, n+1):%0A            ilist.append(i)%0A        for i in xrange(1, n+1):%0A            factor *= i%0A        %0A        k -= 1%0A        while n %3E 0:%0A            factor = factor / n%0A            index = k / factor %0A            result.append(str(ilist%5Bindex%5D))  %0A            # Here, ''.join(result), result must be the string list ! %0A            # result = %5B'1','2','3'%5D%0A            ilist.remove(ilist%5Bindex%5D)%0A            k %25= factor %0A            n -= 1%0A        %0A        return ''.join(result) %0A%0A# For the permutation of n, the total amount is n!%0A# so we could seperate into n group, each group has (n-1)! %0A# if we want to get the kth permutation, k/(n-1)! is the index %0A# among current n groups, need to mention here, k = 1,2,3,4....%0A# but index is based on 0, so here we use k-1, then k/(n-1)!%0A# then we delete that member in the list, k = k %25 (n-1)!%0A# n -= 1; factor = factor / n%0A

Create Permutation_sequence.py