Given num Rows, generate the firstnum Rows of Pascal's triangle.

For example, givennumRows= 5,
Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

URL: https://leetcode.com/problems/pascals-triangle/

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        if numRows <= 0:
            return []

        result = []
        pre = []

        pre.append(1)
        result.append(pre)

        for i in range(0, numRows-1):
            curr = []
            curr.append(1)
            for j in range(0, len(pre)-1):
                curr.append(pre[j]+pre[j+1])    
            curr.append(1)
            result.append(curr)
            pre = curr

        return result

results matching ""

    No results matching ""