题目链接 => 戳这里
解法
// 今天的这种做法是在原数组上直接修改值
// 因为每个元素只会被遍历使用一次,所以直接修改值得做法无关紧要
class Solution {
public int minPathSum(int[][] grid) {
int row = grid.length;
int col = grid[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (i == 0 && j == 0) {
continue;
} else if (i == 0) {
grid[i][j] = grid[i][j-1] + grid[i][j];
} else if (j == 0) {
grid[i][j] = grid[i-1][j] + grid[i][j];
} else {
grid[i][j] = Math.min(grid[i-1][j], grid[i][j-1]) + grid[i][j];
}
}
}
return grid[row-1][col-1];
}
}