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.,  +  +  +  = 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 < triangle[i].size(); ++j)
            {
                triangle[i][j] += min(triangle[i + 1][j], triangle[i + 1][j + 1]);
            }
        }
        return triangle[0][0];
    }
};

  

LeetCode 之 Triangle的更多相关文章

  1. LeetCode 120. Triangle (三角形)

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

  2. [LeetCode] Valid Triangle Number 合法的三角形个数

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  3. [LeetCode] Largest Triangle Area 最大的三角区域

    You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...

  4. LeetCode Valid Triangle Number

    原题链接在这里:https://leetcode.com/problems/valid-triangle-number/description/ 题目: Given an array consists ...

  5. [Leetcode Week8]Triangle

    Triangle 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/triangle/description/ Description Given a t ...

  6. 【leetcode】Triangle (#120)

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

  7. [LeetCode][Java]Triangle@LeetCode

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

  8. LeetCode - 120. Triangle

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

  9. 【leetcode】triangle(easy)

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

  10. leetcode 120 Triangle ----- java

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

随机推荐

  1. javaapplicationWeb application setup on Ubuntu VPS

    题记:写这篇博客要主是加深自己对javaapplication的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. Now there are many hosting server ...

  2. 史上最“脑残”的“抢火车票”程序(node.js版)

    [背景] 快过年了,我妈一个电话打过来叫我给他买火车票,我到12306一查,硬座和硬卧基本没有了,高铁又太贵. 最后只抢了3张无座票,但是我妈说能不能买有座位的啊,我说没有了啊,我妈:你过两天再帮我看 ...

  3. java异常处理01

    当我们做java项目的时候,多多少少都会出现一些异常,如何快速处理异常也将会影响到一个项目开发的进度. 以下将是面对的一些异常将如何去处理: 1.数据库没有启动 解决方法:计算机-->管理--& ...

  4. LightOJ 1248 Dice (III)

    期望,$dp$. 设$dp[i]$表示当前已经出现过$i$个数字的期望次数.在这种状态下,如果再投一次,会出现两种可能,即出现了$i+1$个数字以及还是$i$个数字. 因此 $dp[i]=dp[i]* ...

  5. jQuery插件slides实现无缝轮播图特效

    初始化插件: slides是一款基于jQuery无缝轮播图插件,支持图内元素动画,可以自定义动画类型 1 2 3 4 5 6 7 8 9 10 $(".slideInner").s ...

  6. FineReport中以jws方式调用WebService数据源方案

    在使用WebService作为项目的数据源时,希望报表中也是直接调用这个WebService数据源,而不是定义数据连接调用对应的数据库表,这样要怎么实现呢? 在程序中访问WebService应用服务, ...

  7. HDU 4403 A very hard Aoshu problem(DFS)

    A very hard Aoshu problem Problem Description Aoshu is very popular among primary school students. I ...

  8. C#中对属性和字段的理解

    字段 C#中很少提到全局变量的概念, 引入了字段一词来代替全局变量, 但也并不是这样简单的, 字段会比全局变量的使用更难理解, 使用上会简单, 初学者当做成员变量或者全局变量不会有什么影响, 随着使用 ...

  9. redis单主机多实例

    假设我们服务器上面已经安装好了redis: 可参看:http://zlyang.blog.51cto.com/1196234/1834700 下面我们来配置redis单主机多实例: 我们首先拷贝两份文 ...

  10. 3.使用secureCRT连接PC,LINUX,开发板

    1.设置secureCRT(可选项):http://www.linuxyw.com/linux/gongxiang/20130505/161.html 2.使用secureCRT远程登录linux 3 ...