class Solution {
public int minPathSum(int[][] grid) {
//创建一维数组用于缓存动态规划的状态
int[] dp = new int[grid[0].length];
//给一维数组赋初值
dp[0] = grid[0][0];
for(int i = 1;i < grid[0].length;i++){
dp[i] = dp[i - 1] + grid[0][i];
}
//动态规划迭代出每个i,j的值
for(int i = 1; i < grid.length;i++){
dp[0] = dp[0] + grid[i][0];
for(int j = 1;j < dp.length;j++){
dp[j] = Math.min(dp[j - 1],dp[j]) + grid[i][j];
}
}
//返回结果
return dp[dp.length - 1];
}
}
```