当前位置:网站首页>[dynamic programming] Yang Hui triangle
[dynamic programming] Yang Hui triangle
2022-04-23 07:17:00 【Breeze_】
Given a nonnegative integer *
numRows,* Generate 「 Yang hui triangle 」 BeforenumRowsThat's ok .stay 「 Yang hui triangle 」 in , Each number is the sum of the numbers at the top left and right of it .
link :https://leetcode-cn.com/problems/pascals-triangle/
Definition : d p [ i ] [ j ] dp[i][j] dp[i][j] Indicates the index position i , j i,j i,j Value
The boundary conditions : 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
State transition equation : 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 Not a boundary , The boundary is 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: # That's ok
if curRow==0:
dp.append([1])
elif curRow==1:
dp.append([1,1])
else:
temp = []
for i in range(curRow + 1): # Column
if i==0 or i == curRow: # If a boundary is encountered
temp.append(1)
else:
temp.append(dp[curRow-1][i]+dp[curRow-1][i-1])
dp.append(temp)
curRow += 1
return dp
If you only need a sequence index of numRows( Index from 0 Start ) Value , So based on the above code , Just make a simple change , relevant link
class Solution:
def getRow(self, numRows: int) -> List[int]:
dp = []
curRow = 0
while curRow < numRows+1: # That's ok
if curRow==0:
dp=[1]
elif curRow==1:
dp=[1,1]
else:
temp = []
for i in range(curRow + 1): # Column
if i==0 or i == curRow: # If a boundary is encountered
temp.append(1)
else:
temp.append(dp[i]+dp[i-1])
dp=temp
curRow += 1
return dp
版权声明
本文为[Breeze_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610323388.html
边栏推荐
- MySQL笔记2_数据表
- Data class of kotlin journey
- Android清除应用缓存
- AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
- Itop4412 cannot display boot animation (4.0.3_r1)
- 补补网络缺口
- [2021 book recommendation] kubernetes in production best practices
- Miscellaneous learning
- 第8章 生成式深度学习
- 【動態規劃】不同路徑2
猜你喜欢

微信小程序 使用wxml2canvas插件生成图片部分问题记录

Binder mechanism principle

Cancel remote dependency and use local dependency
![[recommendation of new books in 2021] practical IOT hacking](/img/9a/13ea1e7df14a53088d4777d21ab1f6.png)
[recommendation of new books in 2021] practical IOT hacking
![Android interview Online Economic encyclopedia [constantly updating...]](/img/48/dd1abec83ec0db7d68812f5fa9dcfc.png)
Android interview Online Economic encyclopedia [constantly updating...]

一款png生成webp,gif, apng,同时支持webp,gif, apng转化的工具iSparta

ThreadLocal, just look at me!

Itop4412 LCD backlight drive (PWM)

Google AdMob advertising learning

BottomSheetDialogFragment 与 ListView RecyclerView ScrollView 滑动冲突问题
随机推荐
组件化学习(3)ARouter中的Path和Group注解
“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
this. getOptions is not a function
PyTorch中的一些常见数据类型转换方法,与list和np.ndarray的转换方法
深度学习模型压缩与加速技术(一):参数剪枝
Component based learning (1) idea and Implementation
HandlerThread原理和实际应用
三子棋小游戏
[2021 book recommendation] learn winui 3.0
MySQL notes 2_ data sheet
谷歌AdMob广告学习
./gradlew: Permission denied
【动态规划】杨辉三角
Android面试计网面经大全【持续更新中。。。】
Component learning
第8章 生成式深度学习
MySQL5. 7 insert Chinese data and report an error: ` incorrect string value: '\ xb8 \ XDF \ AE \ xf9 \ X80 at row 1`
Markdown basic grammar notes
Itop4412 HDMI display (4.4.4_r1)
Binder mechanism principle