标签:ini 结果 css empty exp 练手 path ons ast
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.
Example:
Input: [ [1,3,1], [1,5,1], [4,2,1] ] Output: 7 Explanation: Because the path 1→3→1→1→1 minimizes the sum.
class Solution { public: int minPathSum(vector<vector<int>>& grid) { if(grid.empty()||grid[0].empty()) return 0; int h=grid.size(),w=grid[0].size(); vector<vector<int>> dp(h,vector<int>(w)); for(int i=0;i<h;++i) for(int j=0;j<w;++j) { if(0==i&&0==j) dp[i][j]=grid[i][j]; else if (0==i&&j) dp[i][j]=dp[i][j-1]+grid[i][j]; else if(0==j&&i) dp[i][j]+=dp[i-1][j]+grid[i][j]; else dp[i][j]+=min(dp[i-1][j],dp[i][j-1])+grid[i][j]; } return dp[h-1][w-1]; } };
标签:ini 结果 css empty exp 练手 path ons ast
原文地址:https://www.cnblogs.com/lychnis/p/11743120.html