class Solution:
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
r = [[1]]
for i in range(1,numRows):
r.append(list(map(lambda x,y:x+y, [0]+r[-1],r[-1]+[0])))
return r[:numRows]
lambda是没有具体名字的匿名函数,map参照http://www.runoob.com/python/python-func-map.html
在杨辉三角中,每个数是它左上方和右上方的数的和。
[0]+r[-1],r[-1]+[0]相加的过程是,在前一行的list中分别在头和尾插入“0”,再将各元素相加。
举个例子,第一行是[1] ,第二行是[0,1]和[1,0]各元素相加的结果,即[1,1]
第二行是[1,1],第三行是[0,1,1]和[1,1,0]各元素相加的结果,即[1,2,1],以此类推。