资讯专栏INFORMATION COLUMN

leetcode64 Minimum Path Sum

BlackHole1 / 2424人阅读

摘要:题目要求计算从左上角到右下角的最短路径长度。只能向下或者向右移动。其它的节点则可以通过左侧或上侧的节点到达。只需要判断左或上哪条路径更短即可。

题目要求
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

计算从左上角到右下角的最短路径长度。只能向下或者向右移动。
类似的题目请参考我的博客Unique Path I 和 Unique Path II

思路和代码

同样,最左侧和最上侧的节点都只有一条路径可以到达,所以它们的路径唯一。其它的节点则可以通过左侧或上侧的节点到达。只需要判断左或上哪条路径更短即可。代码如下:

public int minPathSum(int[][] grid) {
    int row = grid.length;
    if(row==0){
        return 0;
    }
    int column = grid[0].length;
    
    for(int i = 1 ; i


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67233.html

相关文章

  • [LintCode/LeetCode] Minimum Path Sum

    摘要:遍历整个矩阵,对于,取上方和左边较小值,与相加可得该点最小值。 Problem Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. You ...

    CntChen 评论0 收藏0
  • [LintCode/LeetCode] Triangle

    摘要:第一种方法是很早之前写的,先对三角形两条斜边赋值,和分别等于两条斜边上一个点的和与当前点的和。然后套用动规公式进行横纵坐标的循环计算所有点的,再遍历最后一行的,找到最小值即可。 Problem Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen...

    刘德刚 评论0 收藏0
  • leetcode-120-Triangle-等腰三角形

    摘要:题目示例题目解析此题是等腰三角形,上下之间的关系简化为上下相邻的三个数,相邻,大小关系是在下方二选一上方的数值,必然正确。根据此思路,可以或者,由于可以简化,所以动态规划方法。代码普通代码,较慢动态规划,简练 题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may mov...

    MarvinZhang 评论0 收藏0
  • [LeetCode] 120. Triangle

    Problem Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], ...

    stormjun 评论0 收藏0
  • [Leetcode] Triangle 三角形

    摘要:动态规划复杂度时间空间思路这题我们可以从上往下依次计算每个节点的最短路径,也可以自下而上。自下而上要简单一些,因为我们只用在两个下方元素中选一个较小的,就能得到确定解。 Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent ...

    jayce 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<