leetcode:Minimum Path Sum
Posted 自朗活
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode:Minimum Path Sum相关的知识,希望对你有一定的参考价值。
不大懂
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]; }
以上是关于leetcode:Minimum Path Sum的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode: Minimum Falling Path Sum