首页 > 代码库 > Minimum Path Sum

Minimum Path Sum

问题:从左上角到右下角的最小路径和

class Solution {public:    int num[300][300];    int dfs(int x,int y,vector<vector<int> >&grid)    {        if(x==grid.size()-1 && y==grid[0].size()-1) return grid[x][y];        if(num[x][y]) return num[x][y];        int h1=99999,h2=99999;        if(x+1<grid.size())  h1=dfs(x+1,y,grid);        if(y+1<grid[0].size()) h2=dfs(x,y+1,grid);        return num[x][y]=min(h1,h2)+grid[x][y];    }    int minPathSum(vector<vector<int> > &grid) {        memset(num,0,sizeof(num));        return dfs(0,0,grid);    }};