1 题目描述
给出一个三角形,计算从三角形顶部到底部的最小路径和,每一步都可以移动到下面一行相邻的数字,
2 解题思路
自底向上。
3 代码实现
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
vector<int> total(triangle.back());
for(int i = triangle.size() - 2; i >= 0; i--){
for(int j = 0; j < triangle[i].size(); j++){
total[j] = triangle[i][j] + min(total[j], total[j + 1]);
}
}
return total[0];
}
};
std::vector::back
reference back();
Access last element
Parameters
none
Return
A reference to the last element in the vector.
4 运行结果
运行时间:3ms
占用内存:504k