minimum-path-sum
2019-07-22 本文已影响0人
DaiMorph
因为限定了只能下和右,所以第一行和第一列边界就是直着走
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
int m=grid.size(),n=grid[0].size();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(i&&j==0)grid[i][j]=grid[i-1][j]+grid[i][j];
if(j&&i==0)grid[i][j]=grid[i][j-1]+grid[i][j];
if(i&&j)grid[i][j]=min(grid[i-1][j],grid[i][j-1])+grid[i][j];
}
}
return grid[m-1][n-1];
}
};