这是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. python中的动态变量

    def make_name(): names = locals() for i in range(1, 10): names['t%s' % i] = i print names['t%s' % i]

  2. LoadRunner ERROR:Could not call flex.messaging.io.amf.ASObject.readObject() : Cannot parse date.

    Error: Encoding of AMF message failed. Error is : Exception Occurred while invoking WriteObject meth ...

  3. 解决javascript动态改变img的src属性图片不显示问题

    首先讲下这个bug的出现的情况,页面中有<a href="JavaScript:void(0)" onclick="document.getElementById( ...

  4. C#设置通过代理访问ftp服务器

    // 创建FTP连接 private FtpWebRequest CreateFtpWebRequest(string uri, string requestMethod) { FtpWebReque ...

  5. 《精通C#》第十三章 对象的生命周期

    在C#中,程序员无法直接在C#中删除一个托管对象,因为C#不提供这个功能,那么类的实例就需要通过CLR调用垃圾回收机制进行清除,回收内存..NET垃圾回收器会压缩空的内存块来实现优化,为了辅助这一功能 ...

  6. 前端面试题2016--HTML

    本文由我收集总结了一些前端面试题,初学者阅后也要用心钻研其中的原理,重要知识需要系统学习.透彻学习,形成自己的知识链.万不可投机取巧,临时抱佛脚只求面试侥幸混过关是错误的!也是不可能的!不可能的!不可 ...

  7. 浅析final 关键字

    谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来了解final这个关键字的用法.下 ...

  8. ASP.NET MVC 多语言实现——URL路由

    考虑实现一个完整的基于asp.net mvc的多语言解决方案,从路由到model再到view最后到数据库设计(先挖好坑,后面看能填多少). 我所见过的多语言做得最好的网站莫过于微软的msdn了,就先从 ...

  9. Java条件语句练习

    /*System.out.println("请输入三个数字:");//输入三个数字,返回最大的那个. int a,b,c,big; Scanner d = new Scanner( ...

  10. GPS定位 测试

    public class MainActivity extends Activity { private final String TAG = "BX"; private Loca ...