Merge pull request #497 from smartAndgoodMark/patch-1

Update Leetcode 题解.md
This commit is contained in:
郑永川 2018-12-17 18:25:13 +08:00 committed by GitHub
commit 4b35ec32a3
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[] 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]);
}
dp[j] += grid[i][j];
} }
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) {
if(j==0) {
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];
}
``` ```
**矩阵的总路径数** **矩阵的总路径数**