LeetCode 64. Minimum Path Sum

2019-09-18 10:47:21 浏览数 (1)

题目

简单动态规划

代码语言:javascript复制
class Solution {
public:
    int dp[1005][1005];
    int minPathSum(vector<vector<int>>& grid) {
        
        int n = grid.size();
        int m = grid[0].size();
        dp[0][0]=grid[0][0];
        for(int i=0;i<n;i  )
        {
            for(int j=0;j<m;j  )
            {
                if(i==0&&j==0)
                    continue;
                if(i==0){
                    dp[i][j] = dp[i][j-1]   grid[i][j];
                    continue;
                }
                if(j==0){
                    dp[i][j] = dp[i-1][j]   grid[i][j];
                    continue;
                }
                
                dp[i][j] = min(dp[i-1][j],dp[i][j-1])   grid[i][j];
            }
        }
        
        return dp[n-1][m-1];
        
    }
};

0 人点赞