LeetCode 118. 杨辉三角

118. 杨辉三角

Difficulty: 简单

给定一个非负整数 _numRows,_生成杨辉三角的前 _numRows _行。

在杨辉三角中,每个数是它左上方和右上方的数的和。

示例:

输入: 5
输出:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

Solution

Language: 全部题目

注意python列表运算的+append()的区别,另外还有res的初始值为[[1]]

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        if not numRows: return []
        res, L = [[1]], [1]
        
        for _ in range(numRows-1):
            L = [sum(i) for i in zip([0] + L, L + [0])]
            res.append(L)
        return res
原文地址:https://www.cnblogs.com/swordspoet/p/14056460.html