【LeetCode】120. Triangle (3 solutions)
Triangle
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.
使用bottom-up方法将最小值汇聚到root,将中间结果保存在开辟的空间curMin中。
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int m = triangle.size();
if(m == )
return ;
else if(m == )
return triangle[][];
vector<int> curMin = triangle[m-];
for(int i = m-; i >= ; i --)
{// for level i
for(int j = ; j <= i; j ++)
{
curMin[j] = triangle[i][j] + min(curMin[j], curMin[j+]);
}
}
return curMin[];
}
};

【LeetCode】120. Triangle (3 solutions)的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】120 - Triangle
原题:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen ...
- 【一天一道LeetCode】#120. Triangle
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode】610. Triangle Judgement
原题 A pupil Tim gets homework to identify whether three line segments could possibly form a triangle. ...
- 【leetcode】Valid Triangle Number
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- 【LeetCode】18. 4Sum (2 solutions)
4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- 【LeetCode】46. Permutations (2 solutions)
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- 【LeetCode】49. Anagrams (2 solutions)
Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- 【LeetCode】77. Combinations (2 solutions)
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
随机推荐
- 跟我学Spring3(9.2):Spring的事务之事务管理器
原文出处: 张开涛9.2.1 概述 Spring框架支持事务管理的核心是事务管理器抽象,对于不同的数据访问框架(如Hibernate)通过实现策略接口PlatformTransactionManage ...
- iOS:UIButton按钮的详解
UIButton的详细介绍: 一.按钮具有的属性: @property(nonatomic,readonly) UIButtonType buttonType; //按钮形状类型 @property ...
- 第八章 ArrayBlockingQueue源码解析
注意:在阅读本文之前或在阅读的过程中,需要用到ReentrantLock,内容见<第五章 ReentrantLock源码解析1--获得非公平锁与公平锁lock()><第六章 Reen ...
- 【属性动画总结】Property Animation
属性动画概述 3.0以前,android仅支持两种动画模式,tweened animation 和 frame-by-frame animation,在android3.0中又引入了一个新的动画系统: ...
- AS .ignore插件 忽略文件
AS自带的.ignore文件 在AS中新建项目时,默认会创建一个.ignore文件,其中默认忽略的是 *.iml .gradle /local.properties /.idea/workspace. ...
- AngularJS 用 $http.jsonp 跨域SyntaxError问题
必须添加参数:callback=JSON_CALLBACK , 才能进success方法,如下: $http.jsonp("https://request.address.json?call ...
- iphone手机微信端html5 Geolocation定位失效的问题
使用Geolocation方法存在错误信息error.POSITION_UNAVAILABLE 其实问题不局限于微信端而是iphone升级到ios10后,对获取地理位置信息作出了限制,只有https的 ...
- Mac下如何进行端口转发,方便一系列需要使用80端口进行的调试工作
上篇文章介绍到,可以在本地hosts文件中添加一条记录将微信公众号中的可信域名解析道本地127.0.0.1,但tomcat在Mac下非root权限80端口是启动不了的,所以我们可以利用pfctl端口转 ...
- json字符串和dict互转
json字符串和dict互转 import json str = '{"params":[{"id":222,"offset":0},{&q ...
- (转)Unity3d UnityEditor编辑器定制和开发插件
在阅读本教程之前,你需要对Unity的操作流程有一些基础的认识,并且最好了解内置的GUI系统如何使用. 如何让编辑器运行你的代码 Unity3D可以通过事件触发来执行你的编辑器代码,但是我们需要一些编 ...