import java.util.*; public class Solution { /** * * @param matrix int整型二维数组 the matrix * @return int整型 */ public int minPathSum (int[][] matrix) { // write code here int r = matrix.length; int c = matrix[0].length; int[][] dp = new int[r][c]; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { if (i == 0 && j == 0) { dp[i][j] = matrix[i][j]; } else if (i == 0) { dp[i][j] = dp[i][j - 1] + matrix[i][j]; } else if (j == 0){ dp[i][j] = dp[i - 1][j] + matrix[i][j]; } else { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + matrix[i][j]; } } } return dp[r - 1][c - 1]; } }