【力扣100】 118.杨辉三角

添加链接描述

思路:

  1. 递推公式是[n,x]=[n-1,x-1]+[n-1,x]
python 复制代码
class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        if numRows==1:
            return [[1]]
        if numRows==2:
            return [[1],[1,1]]
        res=[[1],[1,1]]
        for i in range(2,numRows):  # i代表的是层数的下标(从0层开始)
            cur=[0]*(i+1)
            for j in range(i+1): # j代表的是这层一共有几个数
                if j ==0 or j==i:
                    cur[j]=1
                else:
                    cur[j]=res[i-1][j-1]+res[i-1][j]
            res.append(cur)
        return res
                    
            
相关推荐
田里的水稻几秒前
FA_建图和定位(ML)-超宽带(UWB)定位
人工智能·算法·数学建模·机器人·自动驾驶
Navigator_Z1 分钟前
LeetCode //C - 964. Least Operators to Express Number
c语言·算法·leetcode
郝学胜-神的一滴3 分钟前
Effective Modern C++ 条款40:深入理解 Atomic 与 Volatile 的多线程语义
开发语言·c++·学习·算法·设计模式·架构
摸鱼仙人~8 分钟前
算法题避坑指南:数组/循环范围的 `+1` 到底什么时候加?
算法
liliangcsdn13 分钟前
基于似然比的显著图可解释性方法的探索
人工智能·算法·机器学习
骇城迷影16 分钟前
代码随想录:二叉树篇(中)
数据结构·c++·算法·leetcode
期末考复习中,蓝桥杯都没时间学了30 分钟前
力扣刷题23
算法·leetcode·职场和发展
菜鸡儿齐32 分钟前
leetcode-子集
算法·leetcode·深度优先
今儿敲了吗35 分钟前
28| A-B数对
数据结构·c++·笔记·学习·算法
Desirediscipline38 分钟前
#include<limits>#include <string>#include <sstream>#include <iomanip>
java·开发语言·前端·javascript·算法