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