【120-Triangle(三角形)】


【LeetCode-面试算法经典-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.

题目大意

  给定一个三角形,找出从顶究竟的最小路径和,每一步能够从上一行移动到下一行相邻的数字

解题思路

  递推方程:

  f(0,0)=a[0][0]

  f(i,0)=a[i][0]+f(i-1,0) (i>0)

  f(i,i)=a[i][i]+f(i-1,i-1)(i>0)

  f(i,j)=a[i][j]+MIN(f(i-1,j),f(i-1,j-1))(0

代码实现

算法实现类

import java.util.List;

public class Solution {

    public int minimumTotal(List<List<Integer>> triangle) {

        if (triangle == null || triangle.size() < 1) {
return 0;
}
// 创建数组的第二维度
int[][] minSum = new int[triangle.size()][]; // 创建数组的第一维度
for (int i = 0; i < minSum.length; i++) {
minSum[i] = new int[i + 1];
}
// 设置第一行
minSum[0][0] = triangle.get(0).get(0);
// 设置其他行
for (int i = 1; i < minSum.length; i++) {
List<Integer> line = triangle.get(i);
for (int j = 0; j < minSum[i].length; j++) {
if (j == 0) {
minSum[i][0] = line.get(0) + minSum[i - 1][0];
} else if (i == j) {
minSum[i][j] = line.get(j) + minSum[i - 1][j - 1];
} else if (j < i) {
minSum[i][j] = line.get(j) + Math.min(minSum[i - 1][j], minSum[i - 1][j - 1]);
}
}
}
//找最后一行的最小值就是所求的解
int min = minSum[minSum.length - 1][0];
int length = minSum[minSum.length - 1].length;
for (int i = 1; i < length; i++) {
if (min > minSum[length - 1][i]) {
min = minSum[length - 1][i];
}
} return min;
}
}

评測结果

  点击图片。鼠标不释放。拖动一段位置。释放后在新的窗体中查看完整图片。

特别说明

欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47651229

【LeetCode-面试算法经典-Java实现】【120-Triangle(三角形)】的更多相关文章

  1. 【LeetCode-面试算法经典-Java实现】【118-Pascal&#39;s Triangle(帕斯卡三角形)】

    [118-Pascal's Triangle(帕斯卡三角形(杨辉三角))] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given numRows, generate ...

  2. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  3. 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

    [053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...

  4. 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】

    [062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...

  5. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

  6. 【LeetCode-面试算法经典-Java实现】【136-Single Number(仅仅出现一次的数字)】

    [136-Single Number(仅仅出现一次的数字)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array of integers, ev ...

  7. 【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】

    [075-Sort Colors (颜色排序)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array with n objects colore ...

  8. 【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】

    [101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, check whether ...

  9. 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】

    [109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 ...

随机推荐

  1. Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

    安卓出现如下错误,需要增加FLAG_ACTIVITY_NEW_TASK标志 Intent intent1 = new Intent(getApplicationContext(), CameraAct ...

  2. 【POJ 2976】 Dropping Tests

    [题目链接] http://poj.org/problem?id=2976 [算法] 0/1分数规划 [代码] #include <algorithm> #include <bits ...

  3. React+webpack

    webPack + React 步骤: 1. 创建文件夹 src 源代码目录 main.js 打包的入口文件 App.js 项目的根组件 import React,{Component} from ' ...

  4. [原创]一道基本ACM试题的启示——多个测试用例的输入问题。

    Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离. Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数 ...

  5. GCC G++ Make CMake自我科普

    Linux下gcc g++ make cmake 联系和区别 C/C++程序从编写到可执行一般经历这几个阶段 编写源代码 编译器编译代码生成目标文件,如.o文件 链接器链接目标文件和其他目标文件/库文 ...

  6. H5 微信公众号 监听返回事件

    /*-----监听返回事件-----*/ function pushHistory(returnUrl,currentUrl,currentTitle) { window.addEventListen ...

  7. golang new和make的区别

    自:http://www.cnblogs.com/ghj1976/archive/2013/02/12/2910384.html make用于内建类型(map.slice 和channel)的内存分配 ...

  8. ESLint 规范项目代码

    ESLint 由 JavaScript 红宝书 作者 Nicholas C. Zakas 编写, 2013 年发布第一个版本. NCZ 以可扩展.每条规则独立.不内置编码风格为理念编写了一个 lint ...

  9. C# 响应一个html页面

    System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<html><head ...

  10. hibernate详细配置

    映射配置 <!-- 映射文件: 映射一个实体类对象:  描述一个对象最终实现可以直接保存对象数据到数据库中.  --> <!-- package: 要映射的对象所在的包(可选,如果不 ...