4 lines of Python using formula


  • 0
    S
    def getRow(self, rowIndex):
        ret = [1] * (rowIndex + 1)
        comb_next = lambda x, m, n: x * (m - n + 1) // n
        for n in range(1, rowIndex // 2 + 1):
            ret[n] = ret[-n-1] = comb_next(ret[n-1], rowIndex, n)
        return ret
    

    I thought formula would be faster, but it didn't.

    Why's that?


Log in to reply
 

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