64. 最小路径和

64. 最小路径和


题目链接:64. 最小路径和

代码如下:

//动态规划
class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        for(int i=0;i<grid.size();i++){
            for(int j=0;j<grid[0].size();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]=min(grid[i-1][j],grid[i][j-1])+grid[i][j];
            }
        }
        return grid[grid.size()-1][grid[0].size()-1];
    }
};

你可能感兴趣的:(leetcode,c++)