OJ-Triangle
这是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的更多相关文章
- Leetcode OJ : Triangle 动态规划 python solution
Total Accepted: 31557 Total Submissions: 116793 Given a triangle, find the minimum path sum from ...
- 【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 ...
- 【LeetCode OJ】Pascal's Triangle
Prolbem Link: http://oj.leetcode.com/problems/pascals-triangle/ Just a nest-for-loop... class Soluti ...
- 【LeetCode OJ】Triangle
Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...
- UVa OJ 194 - Triangle (三角形)
Time limit: 30.000 seconds限时30.000秒 Problem问题 A triangle is a basic shape of planar geometry. It con ...
- LeetCode OJ 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 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, ...
- LeetCode OJ 118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- LeetCode OJ:Pascal's Triangle(帕斯卡三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- LeetCode OJ:Triangle(三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- css中vw,vh单位对于UC的兼容性问题
vw,vh单位在移动端浏览器不兼容,在网上找半天也没找到什么官方的解决方法:我就试了一下在使用到vh的单位之前添加一个用px定义的样式: 如: 当浏览器不是别100vw单位的时候 就会赋给px单位的样 ...
- python中在同一个位置输出数据
import sys, time def print_data(): for i in range(5): sys.stdout.write(str(i) + '\r') time.sleep(1) ...
- TensorFlow 在android上的Demo(1)
转载时请注明出处: 修雨轩陈 系统环境说明: ------------------------------------ 操作系统 : ubunt 14.03 _ x86_64 操作系统 内存: 8GB ...
- 切换jdk版本
切换JDK 开发中jdk版本需要是1.3.1,当前电脑中用的是jdk1.8 下载jdk1.3.1 可以在官网上下,但是我捣鼓半天,发现下载又慢又需要账号,所以在CSDN下了个 地址,可以下载第一个ex ...
- Bootstrap 表单
表单布局 Bootstrap 提供了下列类型的表单布局: 垂直表单(默认) 内联表单 水平表单 垂直或基本表单 基本的表单结构是 Bootstrap 自带的,个别的表单控件自动接收一些全局样式.下面列 ...
- ORBSLAM2与OPENCV3.1.0出错解决办法
用opencv3.1.0做ORBSLAM2运行一下命令时cd ORB_SLAM2 chmod +x build.sh ./build.sh出错:/usr/bin/ld: CMakeFiles/mono ...
- Windows Store App 网络通信 HttpWebRequest
如果希望更好地控制HTTP请求,可以使用System.Net类库中的HttpWebRequest类,该类对HTTP协议进行了完整的封装,并且提供了很多对HTTP协议中的 Header.Content和 ...
- 2014 年最热门的国人开发开源软件TOP 100
不知道从什么时候开始,很多一说起国产好像就非常愤慨,其实大可不必.做开源中国六年有余,这六年时间国内的开源蓬勃发展,从一开始的使用到贡献,到推出自己很多的开源软件,而且还有很多软件被国外认可.中国是开 ...
- json序列化时datetime的处理方法
.net自带的json序列化器,JavaScriptSerializer和DataContractJsonSerializer,都是序列化成微软的datetime json格式,e.g. " ...
- 基于SQL Server 2008 Service Broker构建企业级消息系统
注:这篇文章是为InfoQ 中文站而写,文章的地址是:http://www.infoq.com/cn/articles/enterprisemessage-sqlserver-servicebroke ...