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 ...
随机推荐
- Python中的输出
1.Python的输出语句 Python输出语句是print,但是python2.x与3.x又有点区别.python2.x输出 print "xxx"能成功执行,而3.x不行,所以 ...
- 阿里云全民云计算活动:云服务器ECS二折起(云主机)采购指南
首先要注册并登录阿里云,完成实名认证 可以用手机号新注册账号, 也可以使用淘宝账号直接登录,其他的登录方式还支持微博账号和支付宝账号等. 登录后如下图,先点"控制台", 然后鼠标移 ...
- php多个文件上传
表单如下 <form class="form-horizontal" action="{:U('System/addAdvert')}" method=& ...
- 流式数据分析模型kafka+storm
http://www.cnblogs.com/panfeng412/archive/2012/07/29/storm-stream-model-analysis-and-discussion.html ...
- 利用Angular实现多团队模块化SPA开发框架
0.前言 当一个公司有多个开发团队时,我们可能会遇到这样一些问题: 技术选项杂乱,大家各玩各 业务重复度高,各种通用api,登录注销,权限管理都需要重复实现(甚至一个团队都需要重复实现) 业务壁垒,业 ...
- MS10_087漏洞学习研究
类别:栈溢出,fileformat类别漏洞 描述: This module exploits a stack-based buffer overflow in the handling of the ...
- 小程序基于疼讯qcloud的nodejs开发服务器部署
腾讯,疼讯,很疼. 请慎重看腾讯给出的文档,最好做一个笔记. 我只能说我能力有限,在腾讯云小程序的文档中跳了n天. 最后还是觉得记录下来,以防止我的cpu过载给烧了. 此文档是对<小程序 ...
- 一个在java后台实现的对图片进行加网纹或水印的工具类
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- Mysql语句查询优化
其实对Mysql查询语句进行优化是一件非常有必要的事情. 如何查看当前sql语句的执行效率呢? 1.建一张学生表 CREATE TABLE `student` ( `stu_id` ) NOT NUL ...
- JQuery之事件冒泡
JQuery 提供了两种方式来阻止事件冒泡. 方法一:event.stopPropagation(); $("#div1").mousedown(function(event){ ...