当前位置:网站首页>[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
边栏推荐
- Three methods to realize the rotation of ImageView with its own center as the origin
- MySQL笔记3_约束_主键约束
- Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
- ArcGIS License Server Administrator 无法启动解决方法
- Data class of kotlin journey
- MySQL笔记5_操作数据
- [2021 book recommendation] learn winui 3.0
- [2021 book recommendation] kubernetes in production best practices
- 三种实现ImageView以自身中心为原点旋转的方法
- 一款png生成webp,gif, apng,同时支持webp,gif, apng转化的工具iSparta
猜你喜欢

./gradlew: Permission denied

机器学习笔记 一:学习思路

组件化学习(3)ARouter中的Path和Group注解

AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
树莓派:双色LED灯实验
![[2021 book recommendation] artistic intelligence for IOT Cookbook](/img/8a/3ff45a911becb895e6dd9e061ac252.png)
[2021 book recommendation] artistic intelligence for IOT Cookbook

红外传感器控制开关

GEE配置本地开发环境

记录webView显示空白的又一坑

C connection of new world Internet of things cloud platform (simple understanding version)
随机推荐
Android exposed components - ignored component security
Android暴露组件——被忽略的组件安全
MySQL笔记5_操作数据
从0开始封装一套项目的网络请求框架
常见的正则表达式
【动态规划】不同路径2
iTOP4412内核反复重启
iTOP4412 SurfaceFlinger(4.0.3_r1)
torch_geometric学习一,MessagePassing
Component based learning (1) idea and Implementation
C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported
[recommendation for new books in 2021] professional azure SQL managed database administration
Personal blog website construction
Cause: dx. jar is missing
MySQL notes 3_ Restraint_ Primary key constraint
[Exynos4412][iTOP4412][Android-K]添加产品选项
BottomSheetDialogFragment 与 ListView RecyclerView ScrollView 滑动冲突问题
Ffmpeg common commands
【 planification dynamique】 différentes voies 2
取消远程依赖,用本地依赖