题目:
解答:
1 class Solution { 2 public: 3 int maxValue(vector<vector<int>>& grid) 4 { 5 int m = grid.size(); 6 int n = grid[0].size(); 7 8 for (int i = 0; i < m; i++) 9 { 10 for (int j = 0; j < n; j++) 11 { 12 if (i == 0 && j == 0) 13 { 14 continue; 15 } 16 if (i == 0) 17 { 18 grid[i][j] += grid[i][j - 1] ; 19 } 20 else if (j == 0) 21 { 22 grid[i][j] += grid[i - 1][j]; 23 } 24 else 25 { 26 grid[i][j] += std::max(grid[i][j - 1], grid[i - 1][j]); 27 } 28 } 29 } 30 31 return grid[m - 1][n - 1]; 32 } 33 };