moonlightshadow123
6/21/2017 - 8:31 AM

118. Pascal's Triangle

  1. Pascal's Triangle
class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        res = []
        if numRows == 0:
            return res
        res.append([1])
        for i in range(1,numRows):
            # curLength = i + 1
            curRow = [1]*(i+1)
            for j in range(1,i):
                curRow[j] = res[i-1][j-1] + res[i-1][j]
            res.append(curRow)
        return res

https://leetcode.com/problems/pascals-triangle/#/description

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5, Return

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