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