[编程题] lc 最小路径-动态规划

[编程题] lc:最小路径和

题目描述

image-20200723233842689

输入输出

image-20200723233858088

思路

原地动态 规划

image-20200723233940970

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];
    }
}
posted @ 2020-07-23 23:42  北鼻coder  阅读(178)  评论(0编辑  收藏  举报