题目:
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
思路:本题较简单
程序:
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
result = []
if numRows <= 0:
return result
for index1 in range(1, numRows + 1):
data = []
if index1 == 1:
data.append(1)
elif index1 == 2:
data.append(1)
data.append(1)
else:
for index2 in range(0, index1):
if index2 == 0:
data.append(1)
elif index2 > 0 and index2 < index1 - 1:
data.append(result[index1 - 2][index2 - 1] + result[index1 - 2][index2])
else:
data.append(1)
result.append(data)
return result