64.最小路径和

2018-05-15  本文已影响0人  _道友请留步_
class Solution {
    public int minPathSum(int[][] grid) {
        int[][] result = new int[grid.length][grid[0].length];
        result[0][0] = grid[0][0];
        for(int i = 1; i < grid.length; i++){
            result[i][0] = result[i-1][0] + grid[i][0];
        }
        for(int i = 1; i < grid[0].length; i++){
            result[0][i] += result[0][i-1] + grid[0][i];
        }
        for(int i = 1; i < grid.length; i++){
            for(int j = 1; j < grid[0].length; j++){
                result[i][j] = grid[i][j] + Math.min(result[i-1][j], result[i][j-1]);
            }
        }
        return result[result.length-1][result[0].length-1];
    }
}
上一篇 下一篇

猜你喜欢

热点阅读