题解 | #矩阵的最小路径和#
矩阵的最小路径和
http://www.nowcoder.com/practice/7d21b6be4c6b429bb92d219341c4f8bb
class Solution:
def minPathSum(self , matrix ):
m= len(matrix)# 行
n=len(matrix[0])# 列
dp = [[1 for col in range(n)] for row in range(m)]
for i in range(0,m):# 行
for j in range(0,n):# 列
if i==0 and j==0:
dp[i][j]=matrix[0][0]
if i==0 and j!=0:
dp[i][j]=dp[i][j-1]+matrix[i][j]
if i!=0 and j==0:
dp[i][j]=dp[i-1][j]+matrix[i][j]
if i!=0 and j!=0:
dp[i][j]=min(dp[i-1][j],dp[i][j-1])+matrix[i][j]
return dp[m-1][n-1]