leetcode:Minimum Path Sum(路线上元素和的最小值)

2014-11-23 23:11:55 · 作者: · 浏览: 6
题目:
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.
题意从左上到右下,所有可能的路径中,求经过的元素和最小值。
动态规划基础题了,dp每个状态由左边或者上边的值中,较小的值与当前状态的值相加得到。
注意考虑边界情况就行了。
int dp[1000][1000];  
class Solution {  
public:  
    int minPathSum(vector
> &grid) { int rows=grid.size(); if(rows==0)return 0; int cols=grid[0].size(); if(cols==0)return 0; memset(dp,0,sizeof(dp)); for(int i=0;i