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

矩阵的最小路径和

https://www.nowcoder.com/practice/7d21b6be4c6b429bb92d219341c4f8bb?tpId=295&tqId=1009012&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param matrix int整型二维数组 the matrix
# @return int整型
#
class Solution:
    def minPathSum(self , matrix: List[List[int]]) -> int:
        # write code here

        n =len(matrix)
        m =len(matrix[0])

        dp =[[1 for j in range(n)] for i in range(m)]

        for i in range(n):
            for j in range(m):


                if i==0 and j==0:
                    dp[i][j]=matrix[0][0]

                elif i==0 :
                    dp[i][j]=dp[i][j-1]+matrix[i][j]
                elif j==0 :
                    dp[i][j]=dp[i-1][j]+matrix[i][j]
                elif 0<i<m  and 0<j <n:
                    dp[i][j]=min(dp[i-1][j],dp[i][j-1])+matrix[i][j]

        return dp[n-1][m-1]

全部评论

相关推荐

11-18 09:44
Java
小白也想要offer:简历别放洋屁,搞不还还放错了,当然你投外企除外,以上纯属个人观点
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务