力扣(leetcode)第118题杨辉三角(Python)

118.杨辉三角

题目链接:118.杨辉三角

给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。

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

示例 1:

输入: numRows = 5

输出: \[1,1,1,1,2,1,1,3,3,1,1,4,6,4,1]
示例 2:

输入: numRows = 1

输出: \[1]

提示:

1 <= numRows <= 30

解答

python 复制代码
class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        if numRows == 0: return []
        res = [[1]]
        while len(res) < numRows:
            newRow = [a+b for a, b in zip([0]+res[-1], res[-1]+[0])]
            res.append(newRow)      
        return res

最后,我写了一篇MySQL教程,里面详细的介绍了MySQL的基本概念以及操作指令等内容,欢迎阅读!
MySQL数据库万字保姆级教程

相关推荐
Warson_L4 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅4 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅4 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L4 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅4 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L5 小时前
python的类&继承
python
Warson_L5 小时前
类型标注/type annotation
python
ThreeS7 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵9 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏