又是一题动态规划,我获得的是到所有点的最小值,代码如下:
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
int min[1000][1000] = {0};
if(grid.size()<=0) return 0;
int m = grid.size();
int n = grid[0].size();
for(int i = 0;i < m;i++){
for(int j = 0;j < n;j++){
if(i==0&&j==0) min[i][j] = grid[i][j];
else if(i==0) min[i][j] = min[i][j-1]+grid[i][j];
else if(j==0) min[i][j] = min[i-1][j]+grid[i][j];
else
min[i][j] = (min[i][j-1]<min[i-1][j]?min[i][j-1]:min[i-1][j])+grid[i][j];
}
}
return min[m-1][n-1];
}
};