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