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).
动态规划题目
定义一个二维数组记录当前位置到最后一行的数值,然后从下往上算,即s[i][j]代表i,j到最后一行的最小值,这里 选择从下往上是要比从上往下好的
如果从上往下算的话需要判断上一行此处的值是否存在,因为是三角形
* 公式:s[i][j] = t[i][j] + min(s[i+1][j] ,s[i+1][j+1])
* 最后s[0][0]就是最后的结果
public int minimumTotal(List<List<Integer>> triangle) {
int l = triangle.size();
int n = triangle.get(l-1).size();
//特殊情况
if (l ==0 || n==0)
return 0;
int[][] res = new int[l][n];
//初始条件
for (int i = 0;i < n;i++)
{
res[l-1][i] = triangle.get(l-1).get(i);
}
//动态规划主体
for (int i = l-2; i >= 0; i--) {
for (int j = 0;j < triangle.get(i).size();j++)
{
res[i][j] = Math.min(res[i+1][j],res[i+1][j+1]) + triangle.get(i).get(j);
}
}
return res[0][0];
}

[leetcode]120.Triangle三角矩阵从顶到底的最小路径和的更多相关文章

  1. LeetCode 120. Triangle三角形最小路径和 (C++)

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

  2. LeetCode 120. Triangle (三角形最小路径和)详解

    题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...

  3. LeetCode 120. Triangle (三角形)

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

  4. LeetCode - 120. Triangle

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

  5. leetcode 120 Triangle ----- java

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

  6. [LeetCode] 120. Triangle _Medium tag: Dynamic Programming

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

  7. [leetcode 120]triangle 空间O(n)算法

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

  8. Java for LeetCode 120 Triangle

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

  9. [leetcode] 120. Triangle (Medium)

    原题 思路: dp,从下往上依次取得最小的,取到最上面的,就是一条最小的路径. class Solution { public: int minimumTotal(vector<vector&l ...

随机推荐

  1. 初学者值得拥有Hadoop单机模式环境搭建

    单机模式Hadoop环境搭建 Hadoop环境搭建流程图 具体过程 文章目录 单机模式Hadoop环境搭建 Hadoop环境搭建流程图 具体过程 1.搭建准备工作 (1)关闭防火墙 (2)关闭seli ...

  2. day1(ModelViewSet序列化限流排序)

    1.DRF初始化 1.认证 2.权限 3.限流 4.序列化 5.分页 6.版本  7.过滤 8.排序 1.1安装DjangoRestFramework pip install djangoresfra ...

  3. python核心高级学习总结6------面向对象进阶之元类

    元类引入 在多数语言中,类就是一组用来描述如何生成对象的代码段,在python中同样如此,但是在python中把类也称为类对象,是的,你没听错,在这里你只要使用class关键字定义了类,其解释器在执行 ...

  4. PyQt(Python+Qt)学习随笔:containers容器部件GroupBox分组框介绍

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 1.主要属性 GroupBox分组框是一个对多个部件进行编组的框架容器,可以带有标题(title属性 ...

  5. PyQt(Python+Qt)学习随笔:树型部件QTreeWidget中当前列currentColumn和选中项selectedItems访问方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 当前列访问方法 树型部件QTreeWidget的currentColumn()方法返回当前项中得到焦 ...

  6. 2、pytorch——Linear模型(最基础版,理解框架,背诵记忆)(调用nn.Modules模块)

    #define y = X @ w import torch from torch import nn #第一模块,数据初始化 n = 100 X = torch.rand(n,2) true_w = ...

  7. 从输入 URL 到页面展示,这中间发生了什么?

    当面试官问到,请你说说看"从输入 URL 到页面展示,这中间发生了什么?" 以前的我是这样回答的: 用户输入URL后,向服务器端发起请求.如果顺利,得到网络响应之后,浏览器对资源进 ...

  8. jquery和zepto有何区别?

    1.针对移动端程序,Zepto有一些基本的触摸事件可以用来做触摸屏交互(tap事件.swipe事件),Zepto是不支持IE浏览器的. 2.DOM操作的区别:添加id时jQuery不会生效而Zepto ...

  9. setTimeout和setInterval的区别,包含内存方面的分析?

    setTimeout表示间隔一段时间之后执行一次调用,而setInterval则是每间隔一段时间循环调用,直至clearInterval结束. 内存方面,setTimeout只需要进入一次队列,不会造 ...

  10. CC-BY-NC-SA (创作共用许可协议)

    创作共用许可协议 (英语:Creative Commons license,简称CC许可) 是一种公共版权许可协议,其允许分发受版权保护的作品. 一个创作共用许可,用于一个作者想给他人分享.使用.甚至 ...