力扣(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数据库万字保姆级教程

相关推荐
金銀銅鐵22 分钟前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup115 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi007 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵9 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf10 小时前
Agent 流程编排
后端·python·agent
copyer_xyf10 小时前
Agent RAG
后端·python·agent
copyer_xyf10 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf10 小时前
Agent 记忆管理
后端·python·agent
JieE21218 小时前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试