Python easy to understand and not slow solution


  • 0
    P
    class Solution(object):
        def generate(self, numRows):
            """
            :type numRows: int
            :rtype: List[List[int]]
            """
            if numRows == 0:
                return []
            if numRows == 1:
                return [ [1] ]
            if numRows == 2:
                return [ [1], [1,1] ]
            res = [ [1], [1,1] ]
            for _ in range(2, numRows):
                last = res[-1]
                temp = []
                for i in range(len(last)-1):
                    temp.append( last[i]+last[i+1] )
                    # print(temp)
                temp = [1] + temp + [1]
                res.append( temp )
            return res
    

Log in to reply
 

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