题解 | #矩阵的最小路径和#
矩阵的最小路径和
https://www.nowcoder.com/practice/7d21b6be4c6b429bb92d219341c4f8bb
using System;
using System.Collections.Generic;
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param matrix int整型二维数组 the matrix
* @return int整型
*/
public int minPathSum(List<List<int>> matrix) {
// write code here
if (matrix == null)
return 0;
int nH = matrix.Count;
int nL = matrix[0].Count;
int[,] matrixR = new int[nH, nL];
matrixR[0, 0] = matrix[0][0];
for (int i = 1; i < nH; i++)
matrixR[i, 0] = matrixR[i - 1, 0] + matrix[i][0];
for (int j = 1; j < nL; j++)
matrixR[0, j] = matrixR[0, j - 1] + matrix[0][j];
for (int i = 1; i < nH; i++) {
for (int j = 1; j < nL; j++) {
matrixR[i, j] += matrix[i][j] + Math.Min(matrixR[i - 1, j], matrixR[i, j - 1]);
}
}
return matrixR[nH - 1, nL - 1];
}
}

