leetcode 64 最小路径和

动态规划,从右下角算到左上角。

class Solution {
public:
    int minPathSum(vector>& grid) {
        int h = grid.size(), w = grid[0].size();
        if(h == 0 && w == 0) return 0;
        int dp[h][w];
        dp[h-1][w-1] = grid[h-1][w-1];
        //cout<= 0; i--){
            dp[h-1][i] = dp[h-1][i+1] + grid[h-1][i];
            //cout<= 0; i--){
            dp[i][w-1] = dp[i+1][w-1] + grid[i][w-1];
            //cout<= 0; i--){
            for(int j = w-2; j >= 0; j--){
                dp[i][j] = min(dp[i+1][j], dp[i][j+1]) + grid[i][j];
                //cout<

 

你可能感兴趣的:(leetcode)