这是Leet Code OJ上面的一道题,关于求从上到下的最小路径。

这是原题链接:https://leetcode.com/problems/triangle/

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).

此题共有两种思路:

1.从上到下;

2.从下到上,这种方法需要修改初始数组(如果不重新建立数组)。

我是用第二种方法解决的:从倒数第二行开始,求出到达此元素的最小路径,覆盖原始数组此处的值。依次类推,可以求出到达最顶端元素的最小路径,即为Triangle[0][0].

(最近在复习宽带无线通信,要考试了--。感觉维特比译码用的也是这种思路,或者说此题用的是维特比译码的思路。哈哈哈~~)

C++编写。

具体代码如下:

class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
for(int i=triangle.size()-2;i>=0;i--)
{
for(int j=0;j<i+1;j++)
{
if(triangle[i+1][j]<triangle[i+1][j+1])
{
triangle[i][j] += triangle[i+1][j];
}
else
{
triangle[i][j] += triangle[i+1][j+1];
}
}
}
return triangle[0][0];
}
}; 

OJ-Triangle的更多相关文章

  1. Leetcode OJ : Triangle 动态规划 python solution

    Total Accepted: 31557 Total Submissions: 116793     Given a triangle, find the minimum path sum from ...

  2. 【LeetCode OJ】Pascal's Triangle II

    Problem Link: http://oj.leetcode.com/problems/pascals-triangle-ii/ Let T[i][j] be the j-th element o ...

  3. 【LeetCode OJ】Pascal's Triangle

    Prolbem Link: http://oj.leetcode.com/problems/pascals-triangle/ Just a nest-for-loop... class Soluti ...

  4. 【LeetCode OJ】Triangle

    Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...

  5. UVa OJ 194 - Triangle (三角形)

    Time limit: 30.000 seconds限时30.000秒 Problem问题 A triangle is a basic shape of planar geometry. It con ...

  6. LeetCode OJ 120. Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. LeetCode OJ 119. Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  8. LeetCode OJ 118. Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  9. LeetCode OJ:Pascal's Triangle(帕斯卡三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  10. LeetCode OJ:Triangle(三角形)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

随机推荐

  1. java 的 linq,不要再写令人讨厌的 for 了!

    package com.ly.linq; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator ...

  2. 19个必须知道的Visual Studio快捷键(转)

    本文将为大家列出在 Visual Studio 中常用的快捷键,正确熟练地使用快捷键,将大大提高你的编程工作效率. 项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Al ...

  3. C#线程通信与异步委托

    线程的通知机制 AutoResetEvent是线程实现通知操作的重要方法.通常,AutoResetEvent用于通知正在等待线程已发生事件,允许线程通过发信号互相通信. AutoResetEvent时 ...

  4. 生产排产表DL-ZPPR002

    *&---------------------------------------------------------------------* *& Report ZPPR002 * ...

  5. 前端常用的几个js判断(一)

    1. 禁止右键点击$(document).ready(function(){ $(document).bind("contextmenu",function(e){ return ...

  6. 雅美尓(yaml)实战

    献给跟我一样对yaml(雅美尓)有婶婶挫败感的同学! 开始第一个pylon工程,我们就跟yaml配置文件开始了不解之缘.yaml是什么?它有哪些规则? 大IBM的文章如是说:YAML 是一种比 XML ...

  7. java 事件处理机制:按下上下左右键控制小球的运动

    /** * 加深对事件处理机制的理解 * 通过上下左右键来控制一个小球的位置 */package com.test3;import java.awt.*;import javax.swing.*;im ...

  8. 编译OpenJDK的笔记

    1.  ERROR: You seem to not have installed ALSA 0.9.1 or higher. 不需要从ALSA官网下载alsa-dev和alsa-drive, ubu ...

  9. HashMap底层实现原理/HashMap与HashTable区别/HashMap与HashSet区别

    ①HashMap的工作原理 HashMap基于hashing原理,我们通过put()和get()方法储存和获取对象.当我们将键值对传递给put()方法时,它调用键对象的hashCode()方法来计算h ...

  10. css文本换行相关属性及解释

    本文摘自 http://www.wufangbo.com/css-qiang-zhi-huan-hang/ 强制换行与强制不换行用到的属性 我们一般控制换行所用到的CSS属性一共有三个:word-wr ...