【leetcode】triangle(easy)
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).
思路:
自底向上,存当前数字加下面两个中比较小的数字,最后顶上的数字就是答案了。不难,直接AC
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
vector<vector<int>> data(triangle.size(), vector<int>());
data[data.size() - ] = triangle[triangle.size() - ];
for(int i = triangle.size() - ; i >= ; i--)
{
for(int j = ; j < triangle[i].size(); j++)
{
int min = (data[i + ][j] < data[i + ][j + ]) ? data[i + ][j] : data[i + ][j + ];
data[i].push_back(min + triangle[i][j]);
}
}
return data[][];
}
【leetcode】triangle(easy)的更多相关文章
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】树(共94题)
[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- 【LeetCode】Triangle 解决报告
[称号] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
- 【LeetCode】Recursion(共11题)
链接:https://leetcode.com/tag/recursion/ 247 Strobogrammatic Number II (2019年2月22日,谷歌tag) 给了一个 n,给出长度为 ...
- 【leetcode】Triangle (#120)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【leetcode】Candy(hard) 自己做出来了 但别人的更好
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 【leetcode】Climbing Stairs (easy)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
随机推荐
- nodejs开发指南读后感
nodejs开发指南读后感 阅读目录 使用nodejs创建http服务器; supervisor的使用及nodejs常见的调式代码命令了解; 了解Node核心模块; ejs模板引擎 Express 理 ...
- Net上传附件大小控控值(转)
Server Error 404 – File or directory not found. The resource you are looking for might have been rem ...
- c语言小知识点
大一时学c语言,总结的一些自己感觉很零碎且容易忘的知识点,不对之处请指正 1.字符串不管中间是否有数值0,结尾一定有数值02.浮点类型的变量存储并不精确3.printf格式串自动右对齐,加负号左对齐4 ...
- JQuery选择器细节-遁地龙卷风
1.层次选择器-子元素选择器 <body> <div> <p>lol</p> <div> <p></p> </ ...
- Jrebel是一套开发环境,用来实现热部署
http://truemylife.iteye.com/blog/1140921 背景与愿景:开发环境下,tomcat对热布署的支持还不够全面,致使开发人员浪费大量时间在重起服务上.为了提高开发效率, ...
- 2016年10月30日--JavaScript语法
1.基本数据类型:字符串.小数.整数.日期时间.布尔型等. 2.变量:[var]定义变量:var a:所有变量定义都用var定义,var是通用的可变类型. 3.类型转换:转为整数:parseInt() ...
- BZOJ 2574: [Poi1999]Store-Keeper
Description 推箱子. \(n,m\leqslant 100\) Sol Tarjan+边双连通分量+BFS. 直接搜索的复杂度是 \(n^6\) 记录人的位置,箱子的位置和转移. 箱子的位 ...
- Implement a TextView with an animation in its left side
In my case, I want to write a TextView with an animation in its left side. ImageView + TextView coul ...
- CI控制器中设置在其它方法中可用的变量
开发过程中,某些变量可能需要被控制器中的其它方法所调用,这个变量改怎么设置呢? 其实可以用ci的$this->load->vars($array);和$this->load-> ...
- phpcms分页用法简介
PHPCMS分页的用法 前面需要有引用的list,代码如下: {pc:content action="lists" catid="11" order=" ...