题解 | #杨辉三角(一)#
杨辉三角(一)
http://www.nowcoder.com/practice/4385fa1c390e49f69fcf77ecffee7164
class Solution:
def generate(self , num: int) -> List[List[int]]:
# write code here
triangle=[]
triangle.append([1])
for i in range(num-1):
triangle.append([x+y for x,y in zip([0]+triangle[i],triangle[i]+[0])])
return triangle
杨辉三角第n行等于第n-1行分别首尾补0,然后按位相加,如下:
[1,1]=[0,1]+[1,0]
[1,2,1]=[0,1,1]+[1,1,0]
[1,3,3,1]=[0,1,2,1]+[1,2,1,0]
…………