题解 | #矩阵的最小路径和#

矩阵的最小路径和

http://www.nowcoder.com/practice/7d21b6be4c6b429bb92d219341c4f8bb

#
# 
# @param matrix int整型二维数组 the matrix
# @return int整型
#
class Solution:
    def minPathSum(self , matrix ):
        # write code here
        m = len(matrix)
        n = len(matrix[0])
        temp = matrix
        for i in range(0, m):
            for j in range(0, n):
                if(i==0 and j==0):
                    temp[i][j] = temp[i][j]
                elif i==0 and j>0:
                    temp[i][j] += temp[i][j-1]
                elif j==0 and i>0:
                    temp[i][j] += temp[i-1][j]
                else:
                    temp[i][j] += min(temp[i-1][j],temp[i][j-1])
        return temp[m-1][n-1]
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务