跳至主要內容

064. 最小路径和

T4mako算法数组动态规划矩阵小于 1 分钟

064. 最小路径和

题目描述open in new window

简单
class Solution {
    public int minPathSum(int[][] grid) {
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if(i - 1 >= 0 && j - 1 >= 0) grid[i][j] += Math.min(grid[i - 1][j],grid[i][j - 1]);
                else if (i - 1 >= 0) grid[i][j] += grid[i - 1][j];
                else if (j - 1 >= 0) grid[i][j] += grid[i][j - 1];
            }
        }
        return grid[grid.length - 1][grid[0].length - 1];
    }
}
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5