码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode[64]Minimum Path Sum

时间:2015-02-09 15:33:25      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

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) 
{
    int m=grid.size();
    if(m==0)return 0;
    int n=grid[0].size();
    vector<int> ivec(n);
    vector<vector<int>> f(m, ivec);
    f[0][0]=grid[0][0];
    for (int ki=1;ki<m;ki++)
    {
        f[ki][0]=f[ki-1][0]+grid[ki][0];
    }
    for(int kj=1;kj<n;kj++)
    {
        f[0][kj]=f[0][kj-1]+grid[0][kj];
    }
    for (int i=1;i<m;i++)
    {
        for (int j=1;j<n;j++)
        {
            f[i][j]=min(f[i-1][j],f[i][j-1])+grid[i][j];
        }
    }
    return f[m-1][n-1];
}
};

 

leetcode[64]Minimum Path Sum

标签:

原文地址:http://www.cnblogs.com/Vae98Scilence/p/4281501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!