My python code used mathritm


  • 0
    Y
    class Solution:
    # @param {integer} n
    # @param {integer} k
    # @return {string}
    def getPermutation(self, n, k):
        num = self.fact(n)
        a=()
        divider = n-1
        dividend = k-1
        initList=range(1,n+1)
        finalList = []
        listconsult=[]
        string =()
        for i in range(1,n):
                print divider
                print dividend
                listconsult.append(dividend/self.fact(divider))
                dividend = dividend%self.fact(divider)
                divider = divider-1
        listconsult.append(0)
        length = len(listconsult)
        for i in range(0,n):
            finalList.append(str(initList.pop(listconsult[i])))
        return "".join(finalList)
    
    def fact(self,n):
        if n==0:
            return 1
        else:
            return n*self.fact(n-1)

Log in to reply
 

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