[leetcode]120. Triangle
2016-07-14 21:17
288 查看
题目:
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
The minimum path sum from top to bottom is
11).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
分析:动态规划,从后往前推,会轻松很多。
代码:
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
for (int i = triangle.size() - 2; i >= 0; --i)
for (int j = 0; j <= i; ++j){
triangle[i][j] +=min(triangle[i+1][j+1],triangle[i+1][j]);
}
return triangle[0][0];
}
};
PS.手机屏幕碎了,不开心
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[ [2], [3,4], [6,5,7], [4,1,8,3] ]
The minimum path sum from top to bottom is
11(i.e., 2 + 3 + 5 + 1 =
11).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
分析:动态规划,从后往前推,会轻松很多。
代码:
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
for (int i = triangle.size() - 2; i >= 0; --i)
for (int j = 0; j <= i; ++j){
triangle[i][j] +=min(triangle[i+1][j+1],triangle[i+1][j]);
}
return triangle[0][0];
}
};
PS.手机屏幕碎了,不开心
相关文章推荐
- [android-x86-6.0-rc1] /system/xbin/log.sh
- 1.2线程的优点
- 制作wifi 小车,开始啦
- webstorm使用正则表达式替换
- 7月4日上午锐捷特岗电话面试
- java 读写锁
- 百度编辑器(Ueditor)最新版(1.4.3.3)插入锚点失败原因分析及BUG修复
- 学习Spring AOP
- Numpy ConfigParser.MissingSectionHeaderError: File contains no section headers.
- iOS中为网站添加图标到主屏幕
- Reverse Nodes in k-Group
- 6月28号涂博面试总结
- Servlet的页面跳转
- <LeetCode OJ> 374. Guess Number Higher or Lower
- Count of Smaller Number before itself
- spring IOC容器接口体系简介
- Reverse String
- 对无限图片轮播器的封装,一句话代码设置轮播器的属性
- 栈的压入和弹出序列
- 6月28号中兴面试一面二面问题