0064.cpp (498B)
1 class Solution { 2 public: 3 int minPathSum(vector<vector<int>> &grid) { 4 int n = grid.size(), m = grid[0].size(); 5 6 for (int i = 1; i < n; i++) 7 grid[i][0] += grid[i - 1][0]; 8 for (int j = 1; j < m; j++) 9 grid[0][j] += grid[0][j - 1]; 10 11 for (int i = 1; i < n; i++) { 12 for (int j = 1; j < m; j++) { 13 grid[i][j] += min(grid[i - 1][j], grid[i][j - 1]); 14 } 15 } 16 17 return grid[n - 1][m - 1]; 18 } 19 };