这是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. 解决driver.findElement(By)运行到此处报null指针问题

    1.由于自动化页面上的元素定位太多,主要是通过By来定位,而By提供了id,xpath,name差不多就可以定位到元素 可以使用一个配置文件存储页面上的定位By值,然后从配置文件获取by值,行程by方 ...

  2. 268. Missing Number -- 找出0-n中缺失的一个数

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  3. SharePoint Foundation 2013 安装出错

    前段时间装foundation 13的时候遇到这个问题.怀疑是Office的问题.然后找了一些资料,问题得到了解决 解决方案: 运行 regedit,删除注册表下的office的LicenseType ...

  4. 安装自创建的windows服务。

    安装自创建的windows服务. 使用工具InstallUtil.exe进行安装和卸载创建的windows服务 安装:C:/WINDOWS/Microsoft.NET/Framework/v2.0.5 ...

  5. UEditor手动调节其宽度

    其高度一般不考虑,给个初始高度,然后任其自动扩展就行,对于其宽度,有两种思路,一种是调节其所在的DIV的宽度,让其自动填充,另一种是直接调节编辑器的宽度: adjust_editor_size: fu ...

  6. LR手动关联新手总结

    最近学习LoadRunner的时候深刻体会:新手入门真心不容易啊 今天一直在纠结LoadRunner的手动关联问题,之前刚开始看书的时候就看到了,不过当时想先放放,后面来细细研究, 今天看的时候在网上 ...

  7. javascript中对象在OOP方面的一些知识(主要是prototype和__proto__相关)

    在ES6的Class到来之前,先总结下个人对js中prototype属性的理解. 1.构造函数(大写函数名  this 无return)   2.原型对象(函数.prototype)  3.实例对象( ...

  8. Zadig - USB driver installation made easy

    http://zadig.akeo.ie/

  9. javascript温故知新

    1 javascript作用域 初学javascript的时候,变量的作用域就感觉有些麻烦,他不像C#或java那样清晰明了,貌似处处都在作用域内,但有时会处处都是空. javascript中,变量的 ...

  10. cacti结合nagios

    使用系统ubuntu12.0.45 监控软件,cacti 使用的是源码安装系统自带的版本过低需要添加插件 nagios采用的系统自带版本 安装nagios apt-get install nagios ...