Minimum Path Sum

/*
比较简单的动态规划
*/
class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {
        int row = grid.size();
        int col = grid[0].size();
        for(int i = 1 ; i < row ; i ++) grid[i][0]+=grid[i-1][0];
        for(int i = 1 ; i < col ; i ++) grid[0][i]+=grid[0][i-1];
        for(int i = 1 ; i < row ; i ++){
            for(int j = 1 ; j < col ; j++){
                grid[i][j] = min(grid[i][j-1],grid[i-1][j])+grid[i][j];
            }
        }
        return grid[row-1][col-1];
    }
};

 

posted @ 2015-04-11 15:17  SprayT  阅读(105)  评论(0编辑  收藏  举报