64. Minimum Path Sum DP经典问题

来源:互联网 发布:淘宝质量鉴定什么意思 编辑:程序博客网 时间:2024/06/05 04:23

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


0 0
原创粉丝点击