Update Leetcode 题解.md
修改矩阵路径代码错误,难以置信这么多人star的项目有这种错误
This commit is contained in:
parent
26d0142b4b
commit
430e1e7eae
|
@ -2544,23 +2544,31 @@ Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes
|
|||
|
||||
```java
|
||||
public int minPathSum(int[][] grid) {
|
||||
if (grid.length == 0 || grid[0].length == 0) {
|
||||
if (grid.length==0 || grid[0].length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int m = grid.length, n = grid[0].length;
|
||||
int[] dp = new int[n];
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (i == 0) {
|
||||
dp[j] = dp[j - 1];
|
||||
} else {
|
||||
dp[j] = Math.min(dp[j - 1], dp[j]);
|
||||
for(int i =0;i<m;i++) {
|
||||
for(int j=0;j<n;j++){
|
||||
if(i==0) {
|
||||
if(j==0) {
|
||||
dp[j] = grid[i][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];
|
||||
}
|
||||
```
|
||||
|
||||
**矩阵的总路径数**
|
||||
|
|
Loading…
Reference in New Issue
Block a user