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.
Idea: This question is very similar to the previous robot's problem, just a little change, the specific code and comments are as follows:
public class Solution {public int minpathsum (int[][] grid) { //Dynamic planning idea //Select the minimum value to this point (minimum value from top and left) for ( int i = 0; i < grid.length; i++) for (int j = 0; J < Grid[0].length; J + +) { if (i > 0 && J > 0)//Three cases, second row after second column Grid[i][j] + = Math.min (grid[i-1][j],grid[i][j-1]); else if (i = = 0 && J > 0)//First line grid[i][j] + = grid[i][j-1]; else if (i > 0 && j==0)//First column grid[i][j] + = Grid[i-1][j]; } Returns the last value return grid[grid.length-1][grid[0].length-1];} }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode 64.Minimum path Sum (shortest path) ideas and methods for solving problems