题解 | #矩阵的最小路径和#
矩阵的最小路径和
http://www.nowcoder.com/practice/7d21b6be4c6b429bb92d219341c4f8bb
动态规划
class Solution { public: int minPathSum(vector<vector<int> >& matrix) { vector<vector<int>> res(matrix); int row = res.size(), col = res[0].size(); for(int i=1; i<row; ++i) res[i][0] += res[i-1][0]; for(int j=1; j<col; ++j) res[0][j] += res[0][j-1]; for(int i=1; i<row; ++i){ for(int j=1; j<col; ++j) res[i][j] += min(res[i-1][j], res[i][j-1]); } return res[row-1][col-1]; } };