leetcode 64. 最小路径和
思路
思路:
遍历矩阵,第一行上面:前后相加。第一列上面:上下相加。中间:比较上下后加上当前值。
代码
class Solution { public: int minPathSum(vector<vector<int>>& grid) { for(int i = 0;i<grid.size();i++) { for(int j = 0;j<grid[0].size();j++) { if(i==0&&j == 0) { continue; } else if(i == 0) { grid[i][j] = grid[i][j] + grid[i][j-1]; } else if(j == 0) { grid[i][j] = grid[i][j]+grid[i-1][j]; } else { grid[i][j] = min(grid[i][j-1],grid[i-1][j])+grid[i][j]; //动态规划 从上往下计算 } } } return grid[grid.size()-1][grid[0].size()-1]; } };
以大多数人努力程度之低,根本轮不到去拼天赋~