题解 | #矩阵的最小路径和#
矩阵的最小路径和
https://www.nowcoder.com/practice/7d21b6be4c6b429bb92d219341c4f8bb
class Solution { public: /** * * @param matrix int整型vector<vector<>> the matrix * @return int整型 */ int minPathSum(vector<vector<int> >& matrix) { int n = matrix.size(); int m = matrix[0].size(); vector<vector<int> > dp(n + 1, vector<int>(m + 1, 0)); for (int i = 1; i < n + 1; i++) { dp[i][1] = matrix[i - 1][0] + dp[i - 1][1]; } for (int j = 1; j < m + 1; j++) { dp[1][j] = matrix[0][j - 1] + dp[1][j - 1]; }// 初始化值 for (int i = 2; i < n + 1; i++) { for (int j = 2; j < m + 1; j++) { dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + matrix[i - 1][j - 1]; } } return dp[n][m]; } };#0offer是寒冬太冷还是我太菜##我的求职思考#