声明:只求用最简单的方法通过,不求优化,不喜勿喷
- 同63还是dp
- 每次取上或者左边的最小值然后加上本体,直接在原数组上操作就行
- 最后取出最右下角的点输出即可
/**
* @param {number[][]} grid
* @return {number}
*/
var minPathSum = function (grid) {
for (var i = 0; i < grid.length; i++) {
for (var j = 0; j < grid[i].length; 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][j - 1], grid[i - 1][j]) + grid[i][j];
}
}
}
return grid[grid.length - 1][grid[0].length - 1]
};