int minimumTotal(vector<vector<int> > &triangle)
{
int n=triangle.size();
for(int layer=n-2; layer>=0; --layer)
{
for(int i=0; i<=layer; ++i)
{
triangle[layer][i] = min( triangle[layer+1][i],triangle[layer+1][i+1]) + triangle[layer][i];
}
}
return triangle[0][0];
}
//下面是更好地方法,是LeetCode讨论里面大神写的,其实容易理解
int minimumTotal(vector<vector<int> > &triangle)
{
int n=triangle.size();//行数
vector<int> minlen(triangle.back());
for(int layer = n-2; layer>=0; --layer)
{
for(int i=0; i<=layer; ++i)
{
minlen[i] = min( minlen[i],minlen[i+1]) + triangle[layer][i];
}
}
return minlen[0];
}