当前位置:网站首页>【动态规划】杨辉三角
【动态规划】杨辉三角
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
边栏推荐
- [多屏互动] 实现双多屏异显二:startActivity方式
- Handlerthread principle and practical application
- 【2021年新书推荐】Professional Azure SQL Managed Database Administration
- 中国各省会城市经纬度位置
- 如何对多维矩阵进行标准化(基于numpy)
- .net加载字体时遇到 Failed to decode downloaded font:
- adb shell top 命令详解
- Itop4412 HDMI display (4.0.3_r1)
- 5种方法获取Torch网络模型参数量计算量等信息
- this. getOptions is not a function
猜你喜欢

winform滚动条美化
![Android interview Online Economic encyclopedia [constantly updating...]](/img/48/dd1abec83ec0db7d68812f5fa9dcfc.png)
Android interview Online Economic encyclopedia [constantly updating...]

微信小程序 使用wxml2canvas插件生成图片部分问题记录
树莓派:双色LED灯实验

Itop4412 HDMI display (4.4.4_r1)
![[recommendation for new books in 2021] professional azure SQL managed database administration](/img/f1/b38cce1dc328a5b534011169909127.png)
[recommendation for new books in 2021] professional azure SQL managed database administration

Binder mechanism principle

C#新大陆物联网云平台的连接(简易理解版)

Cause: dx. jar is missing

Easyui combobox 判断输入项是否存在于下拉列表中
随机推荐
Encapsulate a set of project network request framework from 0
Fill the network gap
./gradlew: Permission denied
Easyui combobox 判断输入项是否存在于下拉列表中
AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
iTOP4412 SurfaceFlinger(4.0.3_r1)
5种方法获取Torch网络模型参数量计算量等信息
[recommendation of new books in 2021] enterprise application development with C 9 and NET 5
[2021 book recommendation] red hat rhcsa 8 cert Guide: ex200
C#新大陆物联网云平台的连接(简易理解版)
MySQL5. 7 insert Chinese data and report an error: ` incorrect string value: '\ xb8 \ XDF \ AE \ xf9 \ X80 at row 1`
js时间获取本周一、周日,判断时间是今天,今天前、后
从0开始封装一套项目的网络请求框架
iTOP4412 SurfaceFlinger(4.4.4_r1)
素数求解的n种境界
iTOP4412 SurfaceFlinger(4.0.3_r1)
机器学习笔记 一:学习思路
Itop4412 LCD backlight drive (PWM)
Explore how @ modelandview can forward data and pages through the source code
利用队列实现栈