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. fist-第一天随笔

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...

  2. Jmeter介绍、下载及配置

    Jmeter介绍: Apache JMeter是Apache组织开发的基于Java的压力测试工具.最初被设计用于Web应用测试,后来扩展到其他测试领域. 它可以用于测试静态和动态资源,例如静态文件.J ...

  3. Fiddler 4 断点调试(修改response请求参数)

    1.选择测试链接 2. 2然后点击规则的Automatic Breakpoints 的 After Responses 3.然后重新发起请求并找到链接 4.然后修改数据 5.最终效果

  4. moviepy音视频剪辑:追踪人脸打马赛克的三种实现方式

    ☞ ░ 前往老猿Python博文目录 ░ 一.引言 在moviepy官网的案例<Tracking and blurring someone's face>和CSDN的moviepy大神uc ...

  5. 老猿Python博文汇总目录--按标题排序

    ☞ ░ 前往老猿Python博文目录 ░ 本部分为老猿CSDN全部博文的汇总(含转载部分),所有文章在此未进行归类,仅按文章标题排序,方便关键字查找.本部分内容将至少以周为单位定期更新,可能不包含发布 ...

  6. PyQt(Python+Qt)学习随笔:QMainWindow的addDockWidget方法增加QDockWidget停靠窗到主窗口

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 DockWidget除了放在QMainWindow窗口内外,也可以放在 ...

  7. C#清除HTML标签方法

    删除字符串中HTML标签代码 public static string ClearHTMLTags1(string HTML) { string[] Regexs ={ @"<scri ...

  8. ripple Failed to load resource: the server responded with a status of 404 (Not Found)

    在VS2015中使用Cordova + typescript开发中,遇到个问题. 在javascript console 中提示: Failed to load resource: the serve ...

  9. Java 线程安全问题的本质

    原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 目录: 线程安全问题的本质 理解CPU JVM虚拟机类比于操作系统 重排序 汇总 一些解释 ...

  10. AtCoder Regular Contest 109

    Contest Link 为什么还没有 Official Editorial 啊--哦,原来是日文题解,那没事了. A - Hands 有两幢 100 层的楼房 \(A,B\) ,将地面所在的楼层称为 ...