Update Leetcode 题解.md

修改矩阵路径代码错误,难以置信这么多人star的项目有这种错误
This commit is contained in:
闫磊&Mark 2018-12-16 21:09:03 +08:00 committed by GitHub
parent 26d0142b4b
commit 430e1e7eae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2544,23 +2544,31 @@ Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes
```java ```java
public int minPathSum(int[][] grid) { public int minPathSum(int[][] grid) {
if (grid.length == 0 || grid[0].length == 0) { if (grid.length==0 || grid[0].length == 0) {
return 0; return 0;
} }
int m = grid.length, n = grid[0].length; int m = grid.length, n = grid[0].length;
int[] dp = new int[n]; int[] dp = new int[n];
for (int i = 0; i < m; i++) { for(int i =0;i<m;i++) {
for (int j = 0; j < n; j++) { for(int j=0;j<n;j++){
if (i == 0) { if(i==0) {
dp[j] = dp[j - 1]; if(j==0) {
} else { dp[j] = grid[i][j];
dp[j] = Math.min(dp[j - 1], dp[j]);
} }
dp[j] += grid[i][j]; else {
dp[j]=dp[j-1] + grid[i][j];
}
}else if(j==0) {
dp[j] = dp[j] + grid[i][j];
}
else{
dp[j] = Math.min(dp[j],dp[j-1])+grid[i][j];
} }
} }
return dp[n - 1]; }
}
return dp[n-1];
}
``` ```
**矩阵的总路径数** **矩阵的总路径数**