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).

public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if (triangle == null) {
return 0;
}
int rowNumber = triangle.size() ;
int [][] minArray = new int [rowNumber][rowNumber];
minArray[0][0] = triangle.get(0).get(0); for (int i = 1; i < rowNumber; i++) {
for (int j = 0; j <= i ; j++) {
if (j == 0)
{
minArray[i][j] = minArray[i-1][j] + triangle.get(i).get(j) ;
} else if (j == i && j != 0)
{
minArray[i][j] = minArray[i-1][j - 1] + triangle.get(i).get(j);
} else {
minArray[i][j] = Math.min(minArray[i- 1][j] + triangle.get(i).get(j), minArray[i- 1][j - 1] + triangle.get(i).get(j));
}
}
}
int min = minArray[rowNumber - 1][0];
for (int i = 0; i < rowNumber; i++) {
if (min > minArray[rowNumber - 1][i]) {
min = minArray[rowNumber - 1][i];
}
}
return min; }
}

Triangle LeetCode |My solution的更多相关文章

  1. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  2. triangle leetcode C++

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

  3. Leetcode OJ : Triangle 动态规划 python solution

    Total Accepted: 31557 Total Submissions: 116793     Given a triangle, find the minimum path sum from ...

  4. [LeetCode][Java]Triangle@LeetCode

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

  5. Triangle leetcode

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

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

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

  7. [LeetCode] All solution

    比较全的leetcode答案集合: kamyu104/LeetCode grandyang

  8. Triangle——LeetCode

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

  9. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

随机推荐

  1. [Redis源码阅读]sds字符串实现

    初衷 从开始工作就开始使用Redis,也有一段时间了,但都只是停留在使用阶段,没有往更深的角度探索,每次想读源码都止步在阅读书籍上,因为看完书很快又忘了,这次逼自己先读代码.因为个人觉得写作需要阅读文 ...

  2. DataSetToJson 扩展方法

    001 using System; 002 using System.Collections.Generic; 003 using System.Linq; 004 using System.Text ...

  3. 《天书夜读:从汇编语言到windows内核编程》八 文件操作与注册表操作

    1)Windows运用程序的文件与注册表操作进入R0层之后,都有对应的内核函数实现.在windows内核中,无论打开的是文件.注册表或者设备,都需要使用InitializeObjectAttribut ...

  4. C语言的第一个程序 “hello world!”

    1,C语言的简介        C语言是一门通用计算机编程语言,应用广泛.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言. ...

  5. python 读取本地文件批量插入mysql

    Uin_phone.txt 本地文件内容 有1000条,这里只是展示前几条,供参考 133584752 133584759 133584764 133584773 133584775 13358477 ...

  6. NetFlow学习笔记

    NetFlow学习笔记 标签: netflow 由于工作需要,对NetFlow做了一些学习和调研,并总结成文档以供学习分享. 背景:随着系统的升级与漏洞的修补,入侵主机进而进行破坏的病毒攻击方式在攻击 ...

  7. 用JAVA中BufferedImage画出漂亮的验证码点击变化

    如果我们想用JAVA中BufferedImage画出漂亮的验证码点击变化怎么实现呢,类似这样: 点击变化,以下是实现过程,直接上代码: 首先前台:<i><img style=&quo ...

  8. SUCTF--WEB

    题目:flag 在哪? 介绍:网址(http://23.106.143.230/a_real_babyweb.php) 1,打开网址,抓包可以发现在 HTTP 头里面有 Cookie:suctf{Th ...

  9. 部分小程序无法获取UnionId原因

    问题背景 通过观察数据,发现有一部分用户是无法获取到UnionId的 也就是接口返回的参数中不包含UnionId参数 看了微信文档的解释,只要小程序在开放平台绑定,就一定会分配UnionId 网上也有 ...

  10. c语言的流程控制

    1. 流程控制 ● 顺序结构:默认的流程结构.按照书写顺序执行每一条语句. ● 选择结构:对给定的条件进行判断,再根据判断结果来决定执行哪一段代码. ● 循环结构:在给定条件成立的情况下,反复执行某一 ...