当前位置:网站首页>[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 」 BeforenumRows
That'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
边栏推荐
- 机器学习 二:基于鸢尾花(iris)数据集的逻辑回归分类
- Component learning (2) arouter principle learning
- [SM8150][Pixel4]LCD驱动
- 【2021年新书推荐】Practical IoT Hacking
- 【动态规划】不同路径2
- Using stack to realize queue out and in
- Using queue to realize stack
- 常见的正则表达式
- “Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
- 【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide
猜你喜欢
【2021年新书推荐】Artificial Intelligence for IoT Cookbook
C#新大陆物联网云平台的连接(简易理解版)
【2021年新书推荐】Learn WinUI 3.0
Record WebView shows another empty pit
Bottom navigation bar based on bottomnavigationview
[recommendation for new books in 2021] professional azure SQL managed database administration
ffmpeg常用命令
JVM basics you should know
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
谷歌AdMob广告学习
随机推荐
Miscellaneous learning
红外传感器控制开关
记录webView显示空白的又一坑
【2021年新书推荐】Red Hat RHCSA 8 Cert Guide: EX200
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
ArcGIS License Server Administrator 无法启动解决方法
iTOP4412无法显示开机动画(4.0.3_r1)
MySQL笔记4_主键自增长(auto_increment)
Component based learning (1) idea and Implementation
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
Handler进阶之sendMessage原理探索
face_recognition人脸检测
MySQL笔记3_约束_主键约束
[Andorid] 通过JNI实现kernel与app进行spi通讯
JVM basics you should know
MarkDown基础语法笔记
5种方法获取Torch网络模型参数量计算量等信息
组件化学习(1)思想及实现方式
BottomSheetDialogFragment + ViewPager+Fragment+RecyclerView 滑动问题
如何对多维矩阵进行标准化(基于numpy)