Leetcode - Minimum Path Sum

题目链接

Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.

解题思路

TODO (稍后补充)

解答代码

class Solution {
public:
    int minPathSum(vector>& grid) {
        if (grid.empty()) return 0;
        int sum=0;
        
        vector > minMatrix(grid.size(), vector(grid[0].size(), 0));
        
        int zeroSum = 0;
        for(int i=0;i= minMatrix[i][j-1] ? minMatrix[i][j-1]+grid[i][j] : minMatrix[i-1][j]+grid[i][j];
            }
        }
        
        return minMatrix[grid.size()-1][grid[0].size()-1];
    }
};

你可能感兴趣的:(Leetcode - Minimum Path Sum)