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]
]

 1 class Solution(object):
 2     def generate(self, numRows):
 3         """
 4         :type numRows: int
 5         :rtype: List[List[int]]
 6         """
 7         res = [[1]]
 8         for i in range(1, numRows):
 9             res += [map(lambda x, y: x+y, res[-1] + [0], [0] + res[-1])]
10         return res[:numRows]

explanation: Any row can be constructed using the offset sum of the previous row. Example:

    1 3 3 1 0 
 +  0 1 3 3 1
 =  1 4 6 4 1
posted on 2017-03-18 17:09  Ci_pea  阅读(181)  评论(0编辑  收藏  举报