题解 | #矩阵的最小路径和#
矩阵的最小路径和
http://www.nowcoder.com/practice/7d21b6be4c6b429bb92d219341c4f8bb
dp
# # # @param matrix int整型二维数组 the matrix # @return int整型 # class Solution: def minPathSum(self , matrix ): # write code here m = len(matrix) if m == 0: return 0 n = len(matrix[0]) if n == 0: return 0 dp = [[0 for _ in range(n)]for _ in range(m)] dp[0][0] = matrix[0][0] for i in range(1, m): dp[i][0] = matrix[i][0] + dp[i-1][0] for j in range(1, n): dp[0][j] = matrix[0][j] + dp[0][j-1] for i in range(1, m): for j in range(1, n): dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + matrix[i][j] return dp[m-1][n-1]