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)的更多相关文章

  1. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  2. 【LeetCode】树(共94题)

    [94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...

  3. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  4. 【LeetCode】BFS(共43题)

    [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...

  5. 【LeetCode】Triangle 解决报告

    [称号] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

  6. 【LeetCode】Recursion(共11题)

    链接:https://leetcode.com/tag/recursion/ 247 Strobogrammatic Number II (2019年2月22日,谷歌tag) 给了一个 n,给出长度为 ...

  7. 【leetcode】Triangle (#120)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. 【leetcode】Candy(hard) 自己做出来了 但别人的更好

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  9. 【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 ...

随机推荐

  1. 大熊君大话NodeJS之开篇------Why NodeJS(将Javascript进行到底)

    一,开篇分析 大家好啊,大熊君又来啦(*^__^*) 嘻嘻……,之前我写过一系列关于JS(OOP与设计模式)方面的文章,反响还好,其实这也是对我本人最大的鼓励,于是我决定我要将JavaScript进行 ...

  2. 清北学堂模拟day4 传球接力

    [问题描述]n 个小朋友在玩传球. 小朋友们用 1 到 n 的正整数编号. 每个小朋友有一个固定的传球对象,第 i 个小朋友在接到球后会将球传给第 ai个小朋友, 并且第 i 个小朋友与第 ai个小朋 ...

  3. DatePicker隐藏年月日

    1.隐藏年 ((ViewGroup) (((ViewGroup) dp.getChildAt(0)).getChildAt(0))) .getChildAt(0).setVisibility(View ...

  4. 【C语言入门教程】5.4 递归

    递归函数 是能够直接或通过另一个函数间接调用自身的函数,调用自身的方法称为递归调用.递归调用的本质是使用同一算法将复杂的问题不断化简,直到该问题解决. 例如求斐波那契数列的某一项算法适用于递归函数实现 ...

  5. Aspose.Cells设置自动列宽(最佳列宽)及一些方法总结

    /// <summary> /// 设置表页的列宽度自适应 /// </summary> /// <param name="sheet">wor ...

  6. 8-IO总结

    3. 4. 5.

  7. Javascript高级程序设计——执行环境与作用域

    Javascript中执行环境是定义了变量或函数有权访问的其他数据,决定了各自的行为,每个执行的环境都有一个与之关联的变量对象,环境中定义的所以变量和函数都保存在这个对象中. 全局执行环境是最外围的一 ...

  8. DP~青蛙过河(hrbust1186)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxoAAAKlCAYAAABMq5pGAAAgAElEQVR4nOzdf4xl53nY9/mrQP8r+k

  9. CCP浅谈

    说明 如果想详细了解CCP,可以下载AN-AMC-1-102_Introduction_to_CCP.pdf或者ccp211.pdf 本文不涉及到专业的知识讲解,如果想查看更加专业的知识可以选择看完以 ...

  10. Android Studio日志工具的使用

    Android Studio的LogCat工具 Verbose:对应Log.v(),这个方法用于打印那些最为琐碎的信息,意义最小的日志信息.是Android日志里面级别最低的一种. Debug:对应L ...