当前位置:网站首页>【动态规划】杨辉三角
【动态规划】杨辉三角
2022-04-23 06:11:00 【小风_】
给定一个非负整数 *
numRows,*生成「杨辉三角」的前numRows行。在「杨辉三角」中,每个数是它左上方和右上方的数的和。
链接:https://leetcode-cn.com/problems/pascals-triangle/
定义: d p [ i ] [ j ] dp[i][j] dp[i][j]表示索引位置 i , j i,j i,j的值
边界条件: d p [ 0 ] [ 0 ] = 1 , d p [ 1 ] [ 0 ] = d p [ 1 ] [ 1 ] = 1 dp[0][0]=1,dp[1][0]=dp[1][1]=1 dp[0][0]=1,dp[1][0]=dp[1][1]=1
状态转移方程: d p [ i ] [ j ] = d p [ i − 1 ] [ j − 1 ] + d p [ i − 1 ] [ j ] , i dp[i][j] = dp[i-1][j-1]+dp[i-1][j]~,~i dp[i][j]=dp[i−1][j−1]+dp[i−1][j] , i不为边界,边界为1
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
# dp[i][j] = dp[i-1][j]+dp[i-1][j-1]
dp = []
curRow = 0
while curRow < numRows: # 行
if curRow==0:
dp.append([1])
elif curRow==1:
dp.append([1,1])
else:
temp = []
for i in range(curRow + 1): # 列
if i==0 or i == curRow: # 如果遇到了边界
temp.append(1)
else:
temp.append(dp[curRow-1][i]+dp[curRow-1][i-1])
dp.append(temp)
curRow += 1
return dp
如果只需要序列索引为numRows(索引从0开始)的值,那么在上面代码基础上,简单改动一下即可,相关链接
class Solution:
def getRow(self, numRows: int) -> List[int]:
dp = []
curRow = 0
while curRow < numRows+1: # 行
if curRow==0:
dp=[1]
elif curRow==1:
dp=[1,1]
else:
temp = []
for i in range(curRow + 1): # 列
if i==0 or i == curRow: # 如果遇到了边界
temp.append(1)
else:
temp.append(dp[i]+dp[i-1])
dp=temp
curRow += 1
return dp
版权声明
本文为[小风_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_33952811/article/details/119328154
边栏推荐
- MySQL notes 2_ data sheet
- iTOP4412 SurfaceFlinger(4.4.4_r1)
- cmder中文乱码问题
- Record WebView shows another empty pit
- 【2021年新书推荐】Kubernetes in Production Best Practices
- 记录webView显示空白的又一坑
- DCMTK (dcm4che) works together with dicoogle
- 最简单完整的libwebsockets的例子
- MySQL笔记4_主键自增长(auto_increment)
- Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
猜你喜欢

Cause: dx. jar is missing

【2021年新书推荐】Practical IoT Hacking

机器学习 二:基于鸢尾花(iris)数据集的逻辑回归分类

【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide

红外传感器控制开关

Android面试计网面经大全【持续更新中。。。】

C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported

Fill the network gap

Itop4412 HDMI display (4.0.3_r1)

Personal blog website construction
随机推荐
Component based learning (1) idea and Implementation
[Andorid] 通过JNI实现kernel与app进行spi通讯
AVD Pixel_2_API_24 is already running.If that is not the case, delete the files at C:\Users\admi
Markdown basic grammar notes
webView因证书问题显示一片空白
扫雷小游戏
Fill the network gap
Ffmpeg common commands
Recyclerview 批量更新View:notifyItemRangeInserted、notifyItemRangeRemoved、notifyItemRangeChanged
[2021 book recommendation] red hat rhcsa 8 cert Guide: ex200
【2021年新书推荐】Practical Node-RED Programming
Google AdMob advertising learning
5种方法获取Torch网络模型参数量计算量等信息
谷歌AdMob广告学习
this.getOptions is not a function
杂七杂八的学习
C connection of new world Internet of things cloud platform (simple understanding version)
[recommendation of new books in 2021] enterprise application development with C 9 and NET 5
winform滚动条美化
MySQL notes 3_ Restraint_ Primary key constraint