【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 ...
随机推荐
- 【转载】Unity 合理安排增量更新(热更新)
原帖地址:由于我看到的那个网站发的这篇帖子很大可能是盗贴的,我就暂时不贴地址了.避免伤害原作者 原版写的有点乱,我个人修改整理了下. --------------------------------- ...
- KNN-实现文本分类
现在大多程序.关于算法的都封装的差不多了... 所以很多程序猿很少来进行深入来研究了... 以前也想过自己好好学习下.但是理论确实难以下咽.怪我喽... 这次项目中需要用到了.要实现对文本进行分类的一 ...
- IGV软件
它支持各种各样的数据类型,包括基于芯片测序.二代测序数据和基因组注释数据等.整合基因组浏览器(IGV,Integrative Genomics Viewer)进行可视化浏览,它支持各种各式的数据类型, ...
- 关系型数据库ACID
关系型数据库ACID 一.事务 定义:所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位. 准备工作:为了说明事务的ACID原理,我们使用银行账户及资金管理的案例 ...
- 在spring容器中定义初始化和销毁bean前所做的操作,有三种方式
1.使用注解,通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 package com.luoq.test.annotation.init; ...
- cocos2dx中CC_CALLBACK_1等宏中this指针实际指向
首先看代码,我在Helloworld中添加两个函数. void HelloWorld::addTarget(){ Size visibleSize = Director::getInstance()- ...
- iOS开发——UI基础-Xcode资源拷贝
#1.拷贝资源的时候选择的copy的含义: 是否要将资源拷贝一份到项目中, 如果不勾选就代表着不拷贝, 那么原来的资源不见了, 项目中的也不能用了 注意: 1.虽然项目中的图片和外部的图片是同一张图片 ...
- Java 反射的应用
在学习反射之前,让我们先了解“类(Class)”.“方法”.“属性”.“类”都是名词,那么相应的在Java中会有这样一些特殊的类:“方法类(Method类)”.“属性类(Field类)”.“构造器类( ...
- Apache Thrift 环境配置
在 Ubuntu 14.04 下Apache Thrift 的安装方法: 1安装依赖包 sudo apt-get install libboost-dev libboost-test-dev libb ...
- PE556
考虑推广sum(i in Z){mu^2(i)}的做法. #include"roundCount.cpp" #include<cstdio> #include<v ...