题解 | #矩阵的最小路径和#

矩阵的最小路径和

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];
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
08-15 01:16
Python
Java小萌新新萌小...:照片不用整这么大的 而且你的照片截歪了 你想找专业对口的 那普通话证写在这里其实没有什么必要 就是看着内容多点 而且里面字体大小也不一样 修改一下排版 有很多空间可以再利用一下 字大一点 不然现在这样观感不太好 再就是项目好好优化一下 加油
点赞 评论 收藏
分享
08-16 10:51
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务