leetcode-64. 最小路径和

leetcode-64. 最小路径和

题目:
leetcode-64. 最小路径和_第1张图片

代码:

#include 
#include 
using namespace std;

int minPathSum(vector<vector<int> >& grid) {
	if(grid.size()==0){
		return 0;
	}
	for(int i=0;i<grid.size();i++){
		for(int j=0;j<grid[0].size();j++){
			if(i==0 && j==0){
				continue;
			}else if(i==0){
				grid[i][j]=grid[i][j-1]+grid[i][j];
			}else if(j==0){
				grid[i][j]=grid[i-1][j]+grid[i][j];
			}else{
				grid[i][j]=(grid[i-1][j]<=grid[i][j-1]?grid[i-1][j]:grid[i][j-1]) +grid[i][j];
			}
		}
	}
	return grid[grid.size()-1][grid[0].size()-1];
}

int main(){
	int res=0;
	vector<vector<int> > grid;
	res=minPathSum(grid);
	cout<<res;
    return 0;
}

你可能感兴趣的:(刷题,leetcode,c++)