分析:
经典动态规划路径求和
就是定义数组有点麻烦,写了一个循环
后面还有边缘问题注意一下就行
i循环从1开始,初始赋值f[0][0]=triangle[0][0]
代码:
class Solution(object): def minimumTotal(self, triangle): """ :type triangle: List[List[int]] :rtype: int """ # 到当前层时的最小和为f[i][j] # min(f[-1]) # f[i]=min(f[i-1][j],f[i-1][j-1])+triangle[i][j] # f[0][0]=triangle[0][0] f=[] for i in range(1,len(triangle)+1): a=[0 for i in range(i)] f.append(a) # return f f[0][0]=triangle[0][0] for i in range(1,len(triangle)): for j in range(len(triangle[i])): if j==0: f[i][j]=f[i-1][j]+triangle[i][j] elif j==i: f[i][j]=f[i-1][j-1]+triangle[i][j] else: f[i][j]=min(f[i-1][j],f[i-1][j-1])+triangle[i][j] return min(f[-1])
标签:triangle,min,int,路径,len,120,range,return,三角形 From: https://www.cnblogs.com/ooooopppp/p/17377808.html