本文主要是介绍64. Minimum Path Sum DP经典问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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.
最小化值
public class Solution {public int minPathSum(int[][] grid) {if(grid.length==0||grid==null) return 0;int m=grid.length;int n=grid[0].length;int [][]res=new int[m][n];for(int i=0;i<m;i++){for(int j=0;j<n;j++){if(i==0&&j==0)res[i][j]=grid[i][j];else if(i==0) res[i][j]=res[i][j-1]+grid[i][j];else if(j==0) res[i][j]=res[i-1][j]+grid[i][j];else res[i][j]=Math.min(res[i][j-1],res[i-1][j])+grid[i][j];}}return res[m-1][n-1];}
}
这篇关于64. Minimum Path Sum DP经典问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!