class Solution {
public int minPathSum(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int[][] dp = new int[m][n];
int sum = 0;
for(int i=0;i<m;i++){
sum+=grid[i][0];
dp[i][0] = sum;
}
sum = 0;
for(int i=0;i<n;i++){
sum+=grid[0][i];
dp[0][i] = sum;
}
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1]) + grid[i][j];
}
}
return dp[m-1][n-1];
}
}
64. 最小路径和
最新推荐文章于 2024-08-29 22:25:44 发布