Minimum Path Sum
这道题很简单了,和前面
Unique path的那两道题差不多。
class Solution { public: int minPathSum(vector<vector<int>>& grid) { if (grid.empty() || grid[0].empty()) return 0; int m=grid[0].size(),n=grid.size(); vector<vector<int>> path(n,vector<int>(m,0)); path[0][0]=grid[0][0]; for(int i=0;i<n;i++) for(int j=0;j<m;j++){ if(i==0&&j>0)path[i][j]=grid[i][j]+path[i][j-1]; if(j==0&&i>0)path[i][j]=grid[i][j]+path[i-1][j]; if(i>0&&j>0){
path[i][j]=min(path[i][j-1]+grid[i][j],path[i-1][j]+grid[i][j]);
}
}
return path.back().back(); }
};
Runtime: 8 ms, faster than 58.99% of C++ online submissions for Minimum Path Sum.