Triangle LeetCode |My solution
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的更多相关文章
- Leetcode Python Solution(continue update)
leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...
- triangle leetcode C++
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Leetcode OJ : Triangle 动态规划 python solution
Total Accepted: 31557 Total Submissions: 116793 Given a triangle, find the minimum path sum from ...
- [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 java(杨辉三角)
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- [LeetCode] All solution
比较全的leetcode答案集合: kamyu104/LeetCode grandyang
- Triangle——LeetCode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
随机推荐
- [Bayesian] “我是bayesian我怕谁”系列 - Variational Autoencoders
本是neural network的内容,但偏偏有个variational打头,那就聊聊.涉及的内容可能比较杂,但终归会 end with VAE. 各个概念的详细解释请点击推荐的链接,本文只是重在理清 ...
- Python爬虫入门:爬虫基础了解
有粉丝私信我想让我出更基础一些的,我就把之前平台的copy下来了,可以粗略看一下,之后都会慢慢出. 1.什么是爬虫 爬虫,即网络爬虫,大家可以理解为在网络上爬行的一直蜘蛛,互联网就比作一张大网,而爬虫 ...
- Java动手动脑——多态和继承
Java动手动脑——继承和多态 实验一 预估输出答案:100 200 201 202 输出结果:100 200 201 202 输出答案分析:100 创建parent类的对象,调用对象的方 ...
- 第一数学归纳法 vs 第二数学归纳法 vs 良序定理
相关: 第一数学归纳法 vs 第二数学归纳法 vs 良序定 第二数学归纳法:硬币问题和堆垛游戏 第一数学归纳法:施塔特中心的地板砖 良序原理:算术基本定理的证明 From : Mathematics ...
- Vue Elementui 如何让输入框每次自动聚焦
在项目优化中碰到一个小问题,在每次提示框显示的时候让提示框中的输入框聚焦.如下图.一般情况下提示框是隐藏的.点击了编辑才会弹出. 那么原生属性autofocus 只在模板加载完成时起作用,也就是说只有 ...
- 你应该学会的Postman用法
postman这个神器相信大家都用过,程序员作为非专业的测试人员,非常需要这么一款简单轻量级的restful测试工具,但是不知道你是否知道,postman的强大之处不只是测试一下接口,还有其他非常赞的 ...
- 【Win 10 应用开发】UI Composition 札记(七):基于表达式的动画
上一篇烂文中,老周给大伙伴们介绍过了几个比较好玩的动画.本篇咱们深化主题,说一说基于表达式的动画.这名字好理解,就是你可以用公式 / 等式来产生动画的目标值.比如,你想让某个可视化对象的高度减半,你的 ...
- 去除HTML选择——兼容IE、FireFox(document.onselectstart,样式)
引之:http://taoistwar.iteye.com/blog/278963 今天做一个拖动效果,在网上找了个模板,作发后发现一拖动就会选择其它页面部分,需要去除这个效果, 找了个模板看了下发现 ...
- ERP中通过自定义单打开流程图
背景: AIO75系统中,制作流程图时选择所属模块,即可在对应模块的左侧列表展示流程图入口. 但在AIO5商务版中没有相关入口,故本文提供使用自定义菜单的方式挂出流程图. 具体步骤: 1.先去看一下是 ...
- linux文件、目录
user用户 group用户组 others其他人用户信息保存在/etc/passwd [root@iZ25het8xn8Z ~]# ls -altotal 56dr-xr-x---. 3 root ...