不大懂
1、
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
2、代码
public int minPathSum(int[][] grid) { if (grid == null || grid.length == 0 || grid[0].length == 0) { return 0; } int M = grid.length; int N = grid[0].length; int[][] sum = new int[M][N]; sum[0][0] = grid[0][0]; //第一列的值,当前值加上前面和 for (int i = 1; i < M; i++) { sum[i][0] = sum[i - 1][0] + grid[i][0]; } //第一行的值,同理 for (int i = 1; i < N; i++) { sum[0][i] = sum[0][i - 1] + grid[0][i]; } //M:行数 for (int i = 1; i < M; i++) {
//N:列数 for (int j = 1; j < N; j++) {
//走最短的路线,数字叫小值 sum[i][j] = Math.min(sum[i - 1][j], sum[i][j - 1]) + grid[i][j]; } } return sum[M - 1][N - 1]; }