美文网首页
110.Minimum Path Sum

110.Minimum Path Sum

作者: 博瑜 | 来源:发表于2017-07-22 15:36 被阅读0次
    public class Solution {
    /**
     * @param grid: a list of lists of integers.
     * @return: An integer, minimizes the sum of all numbers along its path
     */
    public int minPathSum(int[][] grid) {
        // write your code here
        int row = grid.length;
        int column = grid[0].length;
        for (int i = 1; i < column; i++) {
            grid[0][i] = grid[0][i - 1] + grid[0][i];
        }
        for (int i = 1; i < row; i++) {
            grid[i][0] = grid[i - 1][0] + grid[i][0];
        }
        for (int i = 1; i < row; i++) {
            for (int j = 1; j < column; j++) {
                grid[i][j] = grid[i][j] + Math.min(grid[i][j - 1], grid[i - 1][j]);
            }
        }
        return grid[row - 1][column - 1];
    }
    }
    

    相关文章

      网友评论

          本文标题:110.Minimum Path Sum

          本文链接:https://www.haomeiwen.com/subject/tnsukxtx.html