leetCode 64.Minimum Path Sum (最短路) 解题思路和方法

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.

思路:此题和前面几个机器人的题非常相像,只是变化了一点,具体代码和注释如下:

public class Solution {
    public int minPathSum(int[][] grid) {
        //动态规划思想
        //选取到本点的最小值(从上方和左方来的最小值)
        for(int i = 0; i < grid.length; i++)
            for(int j = 0; j < grid[0].length; j++){
                if(i > 0 && j > 0)//分三种情况,第二行第二列之后
                    grid[i][j] += Math.min(grid[i-1][j],grid[i][j-1]);
                else if(i == 0 && j > 0)//第一行
                    grid[i][j] += grid[i][j-1];
                else if(i > 0 && j==0)//第一列
                    grid[i][j] += grid[i-1][j];
            }
        //返回最后一个值
        return grid[grid.length-1][grid[0].length-1];
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-28 12:19:46

leetCode 64.Minimum Path Sum (最短路) 解题思路和方法的相关文章

LeetCode --- 64. Minimum Path Sum

题目链接:Minimum Path Sum 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. 这道题的要求是在m*n

LeetCode 64. Minimum Path Sum 20170515

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. 题目大意:给定一个一个m*n格的方针,每个方格都放一个非负数,找到

LeetCode 64. Minimum Path Sum(最小和的路径)

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. 题目标签:Array 这道题目和前面两题差不多,基本思想都是一样的

LeetCode 64. Minimum Path Sum Java

题目: 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. 题意及分析:给出一个m*n大的网格,每个网格中的数字都为非

19.2.11 [LeetCode 64] Minimum Path Sum

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. Example: Input: [   [1,3,1], [1,5

leetcode 64. Minimum Path Sum(最小路径和)

很典型的动态规划题目 C++解法一:空间复杂度n2 1 class Solution { 2 public: 3 int minPathSum(vector<vector<int>>& grid) { 4 int m=grid.size(),n=grid[0].size(); 5 int dp[m][n]; 6 dp[0][0]=grid[0][0]; 7 for(int i=1;i<m;i++){ 8 dp[i][0]=dp[i-1][0]+grid[i][0];

【Leetcode】Minimum Path Sum

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. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

Leetcode 动态规划 Minimum Path Sum

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Minimum Path Sum Total Accepted: 15789 Total Submissions: 50645My Submissions Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum

Java for LeetCode 064 Minimum Path Sum

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. 解题思路: dp问题,和上一题一样,JAVA实现如下: stati