Merge pull request #497 from smartAndgoodMark/patch-1
Update Leetcode 题解.md
This commit is contained in:
commit
4b35ec32a3
|
@ -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) {
|
||||
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];
|
||||
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) {
|
||||
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];
|
||||
}
|
||||
```
|
||||
|
||||
**矩阵的总路径数**
|
||||
|
|
Loading…
Reference in New Issue
Block a user