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. Input类型是checkbox时checked属性获取

    记录一下checkbox 的 checked 属性的获取办法,以备忘记: 假如你的一个HTML页中有这么一段代码: <input name="chbRem" id=" ...

  2. 再学习之MyBatis

    一.框架基本介绍 1.概念 支持普通SQL查询.存储过程和高级映射,简化和实现了Java 数据持久化层的的开源框架,主要流行的原因在于他的简单性和易使用性. 2.特点 持久层 .ORM(对象关系映射) ...

  3. python 有关datetime时间日期 以及时间戳转换

    直接上代码 其中有注释 #coding=utf-8 import time import datetime def yes_time(): #获取当前时间 now_time = datetime.da ...

  4. Python之子进程subprocess模块

    http://www.cnblogs.com/vamei/archive/2012/09/23/2698014.html http://blog.csdn.net/jgood/article/deta ...

  5. 《阿里巴巴Java工作手册》学习笔记

    最近浏览了一下阿里巴巴的Java开发手册,感觉内容确实非常的赞,发现了不少自己在编程中的误区,因此决定通过成文牢固掌握,文中将选取个人认为比较重要的部分进行描述与分析."愿站在巨人的肩膀上, ...

  6. mybatis中sql语句的批量插入

    <!-- 收件箱插入收件信息 -->    <insert id="insertReceiveemail">           <!-- 生成一条U ...

  7. let和const命令

    let命令 1.let用来声明变量,类似于var,但只在代码块内有效. { let a = 1; var b = 2; } console.log(a); //a is not defined con ...

  8. ECMAScript 6 第一天 let和const命令

    ES6新增声明变量的方法let命令,const命令. (ES5只有两种声明变量的方法:var 命令和 function 命令.) let命令,用来声明变量. 与var声明变量不同于: 1.  let声 ...

  9. 异常:Unknown lifecycle phase "mvn". You must specify a valid lifecycle

    这是在使用maven打包方式启动springboot项目时出现的异常, 我的异常原因属于下面的情况: 此时maven指令行为:mvn spring-boot:run. 如果写成这样会导致最终的mave ...

  10. Less 的使用方法

    Less 的使用方法 Less 可以直接在浏览器端运行(支持IE6+.Webkit.Firefox),也可以借助Node.js或者Rhino在服务端运行. Less是一种动态语言,无论是在浏览器端,还 ...