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 ...
随机推荐
- rabbitmq Clustering Guide--官方
官方文档地址:http://www.rabbitmq.com/documentation.html A RabbitMQ broker is a logical grouping of one or ...
- linux nadianshi
http://www.cnblogs.com/fnng/archive/2012/03/19/2407162.html
- 使用MiniProfiler调试Asp.net Mvc性能
使用nuget添加MiniProfiler.EF组件 在Global文件中配置MiniProfiler protected void Application_Start() { AreaRegistr ...
- 关于xml作为模板的配置服务系统开发
最近在做一个后台配置系统,其实之前也接触过,所谓的配置系统就是指,将你的网站布局抽象成一个xml模板,里面包括你自定义的节点,然后将变化的部分作为配置项,通过服务将配置选项与模板组装成一个js(这个服 ...
- WIN10FTP服务器搭建
在WIN10上搭建FTP服务器 先建立两个文件夹,区分上传和下载,做测试 用 然后在管理--服务界面新建一个用户 用户目录下创建一个用户 因为服务应用程序里面没有IIS,所以我们打开控制面板里面的程序 ...
- div如何加滚动条
<div style="position:absolute; height:400px; overflow:auto"></div>div 设置滚动条显示: ...
- python3下的super()
大家都知道super是用来解决python钻石多重继承出现的基类重复调用的问题,这个就不赘述了,不了解的请点击. 但是我发现还有个问题在于不是钻石继承时继承先后顺序的问题,也就是如果mixin与继承的 ...
- 移动页面缩放方法之(二)控制HTML
<!DOCTYPE HTML> <html lang="zh-cn"> <head> <meta http-equiv="Con ...
- (转)PHP模板smarty简单入门教程
转之--http://blog.163.com/zf_2011@126/blog/static/166861361201062595057962/ 如何在smarty中开始我们程序设计.PHP代码:- ...
- (转)C++静态库与动态库
本文出自 http://www.cnblogs.com/skynet/p/3372855.html 吴秦 什么是库 库是写好的现有的,成熟的,可以复用的代码.现实中每个程序都要依赖很多基础的底层库,不 ...