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

  1. [LeetCode][Java]Triangle@LeetCode

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

  2. Triangle leetcode

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

  3. Pascal's Triangle leetcode

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  4. Triangle LeetCode |My solution

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

  5. Pascal's Triangle leetcode java(杨辉三角)

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  6. Triangle leetcode java

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  7. triangle leetcode C++

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

  8. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  9. LeetCode第二天&第三天

    leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...

随机推荐

  1. 再回首,Java温故知新(七):Java基础之运算符

    常规算数运算符 包括+.-.*./.%分别对应着加.减.乘.除.取余,算数运算符所处理的两个数字必须为同种类型,如果是不同类型且无法进行自动类型转换的话必须进行强制类型转换,不过强制类型转换可能会丢失 ...

  2. 详解Android Handler的使用-别说你不懂handler(转)

    我们进行Android开发时,Handler可以说是使用非常频繁的一个概念,它的用处不言而喻.本文就详细介绍Handler的基本概念和用法. Handler的基本概念         Handler主 ...

  3. Android系统移植与驱动开发--第三章 Git使用入门及在学习中有感

    第三章 Git使用入门 使用Git的目的是减少各种版本的Linux的压缩大小,提供源代码在Linux上进行编译. 在这一个章节中,其实就是关键步骤的操作,虽然Git与我们学习的android没有很大的 ...

  4. NYOJ-520 最大素因子

    这个题基本上就两个知识点, 一个素数筛选法求素数,另一个是求最大公因子, 不过确定最大素数在素数表中的位置时,要用到二分的思想,不然会超时,下面是具体代码的实现; #include <stdio ...

  5. DataTable去重复方法

    //去掉重复行 DataTable table=new DataTable(); DataView dv = table.DefaultView; table = dv.ToTable(true, n ...

  6. UITextView textViewShouldEndEditing

    最近做到UITextView, 在取消键盘事件上我以为和UITextField差不多,于是我这样写: UITextView *textView = [[UITextView alloc] initWi ...

  7. 使用第三方框架 Masonry 实现自动布局

    使用第三方框架 Masonry 实现自动布局 时间:2015-02-10 11:08:41      阅读:4595      评论:0      收藏:0      [点我收藏+] 标签: 由于前两 ...

  8. Listbox与dataGridView的获取鼠标点击事件的区别!!!

    lisxian.SelectedIndex = index; Listbox获取鼠标的代码!!!! DataGridViewRow currenRow = this.dgvxian3.Rows[ind ...

  9. Web学习资源及手册查询整理

    入门了解html.css.js.jQuery:http://www.w3school.com.cn/, bootstrap.nodejs.php.jQuery入门:http://www.runoob. ...

  10. centos U盘安装

    1.版本 LiveCD 和 LiveDVD 是可以直接进入运行系统,类似win PE, 进入系统后有一个图标 install - HHD(从硬盘安装). netinstall 是用于网络安装和系统救援 ...