leetcode 120 Triangle ----- java
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).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
求出最小的路径。
第一次做使用递归,超时了。
- public class Solution {
- public int minimumTotal(List<List<Integer>> triangle) {int len = triangle.size();
- int result = triangle.get(0).get(0);
- result = getResult(result,0,0,triangle);
- return result;
- }
- public static int getResult(int result,int pos,int num,List<List<Integer>> triangle){
- if( num == triangle.size()-1 )
- return result;
- int num1 = triangle.get(num+1).get(pos);
- int ans = result;
- ans += num1;
- ans = getResult(ans,pos,num+1,triangle);
- num1 = triangle.get(num+1).get(pos+1);
- result += num1;
- result = getResult(result,pos+1,num+1,triangle);
- return ans>result?result:ans;
- }
- }
- public class Solution {
- public int minimumTotal(List<List<Integer>> triangle) {
- int height = triangle.size();
- int[] dp = new int[height];
- dp[0] = dp[0]+triangle.get(0).get(0);
- for( int i = 1;i<height;i++){
- int a = dp[0],b = dp[1];
- dp[0] = dp[0]+triangle.get(i).get(0);
- for( int j = 1;j<i;j++){
- dp[j] = Math.min(a,b)+triangle.get(i).get(j);
- a = b;
- b = dp[j+1];
- }
- dp[i] = a+triangle.get(i).get(i);
- }
- int result = dp[0];
- for( int i = 1;i<height;i++)
- result = Math.min(result,dp[i]);
- return result;
- }
- }
leetcode 120 Triangle ----- java的更多相关文章
- 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 ...
- LeetCode 120. Triangle (三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode - 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [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 ...
- [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 ...
- [leetcode] 120. Triangle (Medium)
原题 思路: dp,从下往上依次取得最小的,取到最上面的,就是一条最小的路径. class Solution { public: int minimumTotal(vector<vector&l ...
- LeetCode 120. Triangle三角形最小路径和 (C++)
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- LeetCode 120. Triangle (三角形最小路径和)详解
题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...
- [leetcode]120.Triangle三角矩阵从顶到底的最小路径和
Given a triangle, find the minimum path sum from top to bottom.Each step you may move to adjacent nu ...
随机推荐
- win7 web开发遇到的问题-由于权限不足而无法读取配置文件,无法访问请求的页面
错误一: HTTP Error 500.19 - Internal Server Error配置错误: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (ov ...
- C++指针详解
指针的概念 指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址.要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区,还有指针本身所占 ...
- 记录一些容易忘记的属性 -- UIButton
//设置按钮文字字体(这个只在自定义button时有效) btn1.titleLabel.font = [UIFont systemFontOfSize:30]; showsTouchWhenH ...
- hibernate缓存和提高效率
1.使用二级缓存,多把大批量的.短期多次的查询数据存到二级缓存中,避免和数据库的多次交互,增加负担.二级缓存加在那些增删改少的,查询多的类中.二级缓存的是对象,如果查出来的不是对象,不会放到缓存中去. ...
- 《day06---面向对象入门》
/* java开发流程:思路. 案例:对数组操作.获取最大值. 思路: 1,一组数,要获取最大值,比较. 2,怎么比较?挨个比较,要获取数组中的每一个数据都要比较. 3,比较完,记录下来比较大的数据, ...
- 团队博客——Sprint计划会议1
每日Scrum:第一天 会议时间:4.14.晚八点半 会议地点:基础教学楼一楼大厅 小组成员:郭庆樑,林彦汝,张金 认领人—使团队成员分工合作,保持团队的积极性. ID 名称(NAME) 重要性(IM ...
- c++父类指针强制转为子类指针后的测试(帮助理解指针访问成员的本质)(反多态)
看下面例子: #include "stdafx.h" #include <iostream> class A { //父类 public: void f() / ...
- C++实现python标准库中的Counter
看python standard library by exmple里面提到一个Counter容器,它像muliset一样,能够维持一个集合,并在常量时间插入元素.查询某个元素的个数,而且还提供了一个 ...
- 大型HashMap
看到一篇评估大型HashMap的文章,备份几个Collections库. 原文:Large HashMap overview: JDK, FastUtil, Goldman Sachs, HPPC, ...
- HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn
Children of the Candy Corn Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Jav ...