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 ...
随机推荐
- Android开发艺术探索》读书笔记 (12) 第12章 Bitmap的加载和Cache
第12章 Bitmap的加载和Cache 12.1 Bitmap的高速加载 (1)Bitmap是如何加载的?BitmapFactory类提供了四类方法:decodeFile.decodeResourc ...
- 新一代 PHP 加速插件 Zend Opcache <转>
注: 由于原链接已不存在, 所以我把图片重新整理了一下, 以便看起来更加直观 笔者注: 1> PHP 性能提升之 PHP NG => php next generation wiki ...
- jQuery绑定事件的四种基本方式
Query中提供了四种事件监听方式,分别是bind.live.delegate.on,对应的解除监听的函数分别是unbind.die.undelegate.off. bind(type,[data], ...
- dedecms网站文章标题与简标题的调用问题
使用dedecms调用标签的时候,既然有,咱们就合理利用,如果没有,咱也可以自己去添加.以下介绍dedecms网站文章标题调用的一些技巧,希望大家能够合理运用. dedecms网站文章标题与简标题的调 ...
- php输出金字塔
<?php for($i=0;$i<10;$i++){ for($j=1;$j<=$i;$j++){ echo $i.'*'.$j.'|'; } echo '<br>'; ...
- (转)PHP正则表达式的快速学习方法
1.入门简介 简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或 ...
- Css简介
- CSS 布局Float 【1】
1.HTML元素分类 HTML元素大题可分为内联(inline)元素和块(block)元素. 1.1 内联元素(inline) ①元素显示方式:"文本方式",1个挨着1个,不独自占 ...
- [技术翻译]Guava-libraries(一): 用户指导
用户指导 本文翻译自http://code.google.com/p/guava-libraries/wiki/GuavaExplained,由十八子将翻译,发表于博客园 http://www.cnb ...
- Open vSwitch在openstackHavana概述
最近再看Open vSwitch一些东西,我认为openstack官网上对这一块做了一些了解,所以就把这一块翻译出来以供参考,英语不好,翻译得很粗糙. Open vSwitch插件是最有名的核心插件之 ...