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];
    }
};

  

最新文章

  1. 正确地编写Objective-C中的便捷方法
  2. &quot;mkimage&quot; command not found - U-Boot images will not be built
  3. Centos5, 6下更改系统时间和时区
  4. Visual Studio 2012中Visual Assist破解办法
  5. 常用JQuery插件
  6. HDU 1892-See you(二维BIT)
  7. iOS开发——开发技巧&amp;Mac常用命令
  8. 基于PaaS和SaaS研发的商业云平台实战 转 (今后所有的IT行业会持续集成,往虚拟化方向更快更深的发展,商业化才是这些技术的最终目的)
  9. BZOJ 1085: [SCOI2005]骑士精神(A*算法)
  10. StackExchange.Redis学习笔记(四) 事务控制和Batch批量操作
  11. web项目中js加载慢问题解决思路
  12. CSDN博客添加量子恒道统计代码步骤
  13. Jmeter----创建第一个接口测试流程
  14. C# listview展示表格格式
  15. PTA L1题目合集(更新至2019.3)
  16. 针对需要使用T3协议的Weblogic2628漏洞解决方案
  17. 一个Login页面全面了解session与cookie
  18. [Java in NetBeans] Lesson 07. JavaDoc and Unit Tests
  19. Quartz2D指定显示范围
  20. 【自动化测试】selenium之 chromedriver与chrome版本映射表

热门文章

  1. 用android代码显示图片的一部分源码
  2. 利用GeneratedKeyHolder获得新增数据主键值
  3. 深入浅出Redis-redis底层数据结构(下)
  4. Hibernate缓存配置
  5. python基础4
  6. JS 处理十六进制颜色渐变算法-输入颜色,输出渐变rgb数组
  7. MongoDB应用案例:使用 MongoDB 存储日志数据
  8. openstack私有云布署实践【9.2 Glance镜像管理(办公网环境)】
  9. delphi用TAdoStoredProc调用存储过程,兼容sql2005、2008、2014的远程事务问题
  10. openui5中的RESTful实现odata协议