参考链接:https://blog.nowcoder.net/n/41876a3d44a043f885edbf15ba098a36
import java.util.*; public class Solution { /** * * @param matrix int整型二维数组 the matrix * @return int整型 */ public int minPathSum (int[][] matrix) { int rowLen = matrix.length; int columnLen = matrix[0].length; int[][] dp = new int[rowLen][columnLen]; for (int row = 0; row < rowLen; row++) { for (int column = 0; column < columnLen; column++) { if (row == 0 && column == 0) { dp[row][column] = matrix[0][0]; continue; } if (row == 0 && column != 0) { dp[row][column] = dp[row][column-1] + matrix[row][column]; continue; } if (column == 0) { dp[row][column] = dp[row-1][column] + matrix[row][column]; continue; } dp[row][column] = matrix[row][column] + Math.min(dp[row-1][column], dp[row][column-1]); } } return dp[rowLen-1][columnLen-1]; } }