[编程题] lc 最小路径-动态规划
[编程题] lc:最小路径和
题目描述
输入输出
思路
原地动态 规划
Java代码
class Solution {
//方法:原地dp的动态规划
public int minPathSum(int[][] grid) {
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[i].length;j++){
if(i==0 && j==0){
continue;//还是原值
}else if(i==0 && j>0){
grid[i][j] = grid[i][j-1] + grid[i][j];
}else if(i>0 && j==0){
grid[i][j] = grid[i-1][j] + grid[i][j];
}else{ //i>0 && j>0
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];
}
}