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];
}
}