每天刷一道牛客题霸-第15天-矩阵最小路径和
题目
import java.util.*; public class Solution { /** * * @param matrix int整型二维数组 the matrix * @return int整型 */ public static int minPathSum (int[][] matrix) { if (matrix.length == 0){ return 0; } for (int i = 1 ;i < matrix[0].length ; i++){ matrix[0][i] += matrix[0][i-1]; } for (int i = 1; i < matrix.length ; i++){ matrix[i][0] += matrix[i-1][0]; } for (int i = 1 ; i < matrix.length ; i++){ for (int j = 1 ; j < matrix[i].length ; j++){ matrix[i][j] += Math.min(matrix[i][j-1],matrix[i-1][j]); } } return matrix[matrix.length-1][matrix[0].length-1]; } }#题解##牛客题霸#