auto commit

This commit is contained in:
CyC2018 2018-12-17 18:27:21 +08:00
parent 4b35ec32a3
commit 76ad497f7d

View File

@ -2544,31 +2544,25 @@ 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) {
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];
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 (j == 0) {
dp[j] = dp[j]; // 只能从上侧走到该位置
} else if (i == 0) {
dp[j] = dp[j - 1]; // 只能从左侧走到该位置
} else {
dp[j] = Math.min(dp[j - 1], dp[j]);
}
dp[j] += grid[i][j];
}
}
return dp[n - 1];
}
```
**矩阵的总路径数**