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.
Note: You can only move either down or right at any point in time.
给出一个矩阵,求出从左上角到右下角的路径中的值最小的一个
简单的递推题目,直接看代码
public class Solution {
public int minPathSum(int[][] grid) {
int row = grid.length;
int col = grid[0].length;
for(int i = 0; i < grid.length; ++i){
for(int j = 0; j < grid[0].length; ++j){
if(i == 0){
if(j != 0){
grid[i][j] += grid[i][j - 1];
}
}
else if(j == 0){
if(i != 0){
grid[i][j] += grid[i - 1][j];
}
}
else{
grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]);
}
}
}
return grid[row - 1][col - 1];
}
}