标签:
public class Solution { public int minPathSum(int[][] grid) { if (grid == null || grid.length == 0) { return 0; } int[][] min = new int[grid.length][grid[0].length]; min[0][0] = grid[0][0]; for (int i = 1; i< grid.length; i++) { min[i][0] = min[i - 1][0] + grid[i][0]; } for (int j = 1; j < grid[0].length; j++) { min[0][j] = min[0][j - 1] + grid[0][j]; } for (int i = 1; i< grid.length; i++) { for (int j = 1; j < grid[0].length; j++) { min[i][j] = Math.min(min[i -1][j], min[i][j - 1]) + grid[i][j]; } } return min[grid.length -1][grid[0].length - 1]; } }
DP
min[i][j]= Math.min(min[i-1][j], min[i][j -1]) + grid[i][j]
标签:
原文地址:http://www.cnblogs.com/77rousongpai/p/4523631.html