minimum-path-sum-动态规划

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

找出从左上到右下路径数字之和最小

记得先处理最上和左边一行

class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
for(int i=;i<grid.size();++i)
grid[i][]+=grid[i-][];
for(int j=;j<grid[].size();++j)
grid[][j]+=grid[][j-];
for(int i=;i<grid.size();++i)
{
for(int j=;j<grid[i].size();++j)
{
grid[i][j]+=min(grid[i-][j],grid[i][j-]);
}
}
return grid[grid.size()-][grid[].size()-];
}
};
上一篇:TOJ4127: Root of String


下一篇:BestCoder Round #20 部分题解(A,B,C)(hdu5123,5124,5125)