简介

最小路径和, 使用动态规划?? 感觉我是看出规律来着的

code

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        vector<vector<int>> result(grid.size() + 1, vector<int>(grid[0].size() + 1, -1));
        for(int i=0; i<grid.size(); i++){
            for(int j=0; j<grid[0].size(); j++){
                if(result[i][j+1] == -1 && result[i+1][j] == -1){
                    result[i+1][j+1] = grid[i][j];
                } else if(result[i][j+1] == -1) {
                    result[i+1][j+1] = grid[i][j] + result[i+1][j];
                } else if(result[i+1][j] == -1){
                    result[i+1][j+1] = grid[i][j] + result[i][j+1];
                } else{
                    result[i+1][j+1] = grid[i][j] + min(result[i][j+1], result[i+1][j]);
                }
                
            }
        }
        return result[grid.size()][grid[0].size()];
    }
};
posted on 2021-06-03 23:23  HDU李少帅  阅读(24)  评论(0编辑  收藏  举报