Fastest & shortest solution in Python


  • 1
    H

    Python

    def getRow(self, rowIndex):
            r = [1]
            for i in xrange(1,rowIndex+1):
              r.append( r[-1]*(rowIndex-i+1)/i )
            return r
    

    which can be translated to one line solution

    def getRow1(self, rowIndex):
            return reduce(lambda lst,i: lst + [lst[-1]*(rowIndex-i+1)/i], [[1]] + range(1,rowIndex+1))

Log in to reply
 

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