代码随想录
class Solution {
public int maxValue(int[][] grid) {
int m = grid.length, n = grid[0].length;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(i == 0 && j == 0) continue;
if(i == 0) grid[i][j] += grid[i][j - 1] ;
else if(j == 0) grid[i][j] += grid[i - 1][j];
else grid[i][j] += Math.max(grid[i][j - 1], grid[i - 1][j]);
}
}
return grid[m - 1][n - 1];
}
}
自己写的:
class Solution {
public int maxValue(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if(i>0&&j>0) {
grid[i][j] += Math.max(grid[i-1][j], grid[i][j-1]);
continue;
}
if(j>0) {
grid[i][j] += grid[i][j-1];
}
if(i>0){
grid[i][j] += grid[i-1][j];
}
}
}
return grid[grid.length-1][grid[0].length-1];
}
}