题解 | #矩阵的最小路径和#
矩阵的最小路径和
https://www.nowcoder.com/practice/7d21b6be4c6b429bb92d219341c4f8bb
class Solution { public: int minPathSum(vector<vector<int> >& matrix) { int m = matrix.size(), n = matrix[0].size(); vector<vector<int>> f(m, vector<int>(n)); for (int i = 0; i < m; i++) if (i == 0) f[i][0] = matrix[i][0]; else f[i][0] = f[i - 1][0] + matrix[i][0]; for (int i = 0; i < n; i++) if (i == 0) f[0][i] = matrix[0][i]; else f[0][i] = f[0][i - 1] + matrix[0][i]; for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { f[i][j] = matrix[i][j] + min(f[i][j - 1], f[i - 1][j]); } } return f[m - 1][n - 1]; } };