Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent
numbers on the row below.
1. 开始 temp[i%2] 用的是 temp[(i-1)%2], 出现错误。因为当i=0时,返回的是temp[-1], doesn't make sense!!!
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (triangle.size() == 0)
return 0;
vector<vector<int> > temp;
temp.resize(2);
temp[0].resize(triangle.size());
temp[1].resize(triangle.size());
for(int i = triangle.size()-1; i >= 0; i--){
if (i == triangle.size()-1){
for (int j = 0; j <= i; j++)
temp[(i+1)%2][j] = triangle.at(i).at(j);
}
else{
for (int j = 0; j <= i; j++){
temp[(i+1)%2][j] = min( temp[i%2][j],temp[i%2][j+1])+triangle[i][j];
}
}
}
int sum = temp[1].at(0);
return sum;
}
};
1. 开始 temp[i%2] 用的是 temp[(i-1)%2], 出现错误。因为当i=0时,返回的是temp[-1], doesn't make sense!!!
2.还有一题是打印tree,那个可以用递归或者divide and conquer (看12.07.txt)讨论DP的内容