Triangle——LeetCode
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).
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.
这个题跟之前的这道题几乎是一样的。之前做的用的二维数组保存的,其实由底向上计算,只需要一维数组即可。
public int minimumTotal(List<List<Integer>> triangle) {
if(triangle==null||triangle.size()==0){
return 0;
}
if(triangle.size()==1){
return triangle.get(0).get(0);
}
int[] res = new int[triangle.size()];
for(int i=0;i<triangle.size();i++){
res[i]=triangle.get(triangle.size()-1).get(i);
}
for(int i=triangle.size()-2;i>=0;i--){
List<Integer> row = triangle.get(i);
for(int j = 0;j<row.size();j++){
res[j]=Math.min(res[j],res[j+1])+row.get(j);
}
}
return res[0];
}
Triangle——LeetCode的更多相关文章
- [LeetCode][Java]Triangle@LeetCode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Triangle leetcode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Pascal's Triangle leetcode
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Triangle LeetCode |My solution
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Pascal's Triangle leetcode java(杨辉三角)
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- Triangle leetcode java
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- triangle leetcode C++
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode第二天&第三天
leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...
随机推荐
- TCP/IP源码(59)——TCP中的三个接收队列
http://blog.chinaunix.net/uid-23629988-id-3482647.html TCP/IP源码(59)——TCP中的三个接收队列 作者:gfree.wind@gmai ...
- innode 节点
[root@localhost soft]# ls -i tt1 tt2 xx.c [root@localhost soft]# stat tt1 File: `tt1' Size: 4096 Blo ...
- 转载:IntelliJ Idea 常用快捷键列表
IntelliJ Idea 常用快捷键列表 (http://www.open-open.com/lib/view/open1396578860887.html) Ctrl+Shift + Enter, ...
- jquery之获取当前时间
/** * * 获取当前时间 */ function p(s) { return s < 10 ? '0' + s: s; } var myDate = new Date(); //获取当前年 ...
- sql server 2008 评估期已过期
解决办法: 修改注册表: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\100\ConfigurationState里的&quo ...
- Mysql 中和同to_char 一样用法的函数
STR_TO_DATE() $sql = " SELECT "; $sql .= " m_img,m_content,STR_TO_DATE(m_time,\" ...
- 计时器(Chronometer)的使用
安卓提供了一个计时器组件:Chronometer,该组件extends TextView,因此都会显示一段文本,但是它显示的时间是从某个起始时间开始过去了多少时间,它只提供了android:forma ...
- Java 取整
向上取整用Math.ceil(double a) 向下取整用Math.floor(double a) 举例: public static void main(String[] args) throws ...
- iOS微信支付
SDK接入 服务器签名版本 官方已经是建议使用服务器签名来接入微信支付,实际上从安全上考虑,确实是每个客户端不应该知道RAS密钥,也不需要每个客户端都写一遍签名的算法. 服务端接入流程文档:https ...
- [个人原创]关于java中对象排序的一些探讨(二)
2. 使用Collections.sort()方法 Collections类中提供了诸多静态方法,诸如addAll(),max()等等.当自己相对Collection接口下的类处理的时候,可以看看这 ...