28. Triangle && Pascal's Triangle && Pascal's Triangle II
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).
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.
思想: 经典的动态规划题。
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
vector<int> pSum(triangle.size()+1, 0);
for(int i = triangle.size()-1; i >= 0; --i)
for(int j = 0; j < triangle[i].size(); ++j)
pSum[j] = min(pSum[j]+triangle[i][j], pSum[j+1]+triangle[i][j]);
return pSum[0];
}
};
Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5, Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
思想: 简单的动态规划。
class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int> > vec;
if(numRows <= 0) return vec; vec.push_back(vector<int>(1, 1));
if(numRows == 1) return vec; vec.push_back(vector<int>(2, 1));
if(numRows == 2) return vec; for(int row = 2; row < numRows; ++row) {
vector<int> vec2(row+1, 1);
for(int Id = 1; Id < row; ++Id)
vec2[Id] = vec[row-1][Id-1] + vec[row-1][Id];
vec.push_back(vec2);
}
return vec;
}
};
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,3,1]
.
Note: Could you optimize your algorithm to use only O(k) extra space?
思想: 动态规划。注意O(k)空间时,每次计算新的行时,要从右向左加。否则,会发生值的覆盖。
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> vec(rowIndex+1, 1);
for(int i = 2; i <= rowIndex; ++i)
for(int j = i-1; j > 0; --j) // key, not overwrite
vec[j] += vec[j-1];
return vec;
}
};
28. Triangle && Pascal's Triangle && Pascal's Triangle II的更多相关文章
- pascal+sublime搭建Pascal学习环境
一.fpc安装 1. 下载:http://www.freepascal.org/down/i386/win32.var(或者:http://download.csdn.net/detail/wenph ...
- leetcode—pascal triangle
1.题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- ZOJ - 4081:Little Sub and Pascal's Triangle (结论)
Little Sub is about to take a math exam at school. As he is very confident, he believes there is no ...
- [leetcode] 2. Pascal's Triangle II
我是按难度往下刷的,第二道是帕斯卡三角形二.简单易懂,题目如下: Given an index k, return the kth row of the Pascal's triangle. For ...
- ZOJ-Little Sub and Pascal's Triangle(思维规律)
Little Sub is about to take a math exam at school. As he is very confident, he believes there is no ...
- leetcode 【 Pascal's Triangle II 】python 实现
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- leetcode 【 Pascal's Triangle 】python 实现
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...
- Leetcode_119_Pascal's Triangle II
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41851069 Given an index k, retu ...
- 三角形(Triangle)
三角形(Triangle) 问题 给出一个三角形,找出从顶部至底部的最小路径和.每一步你只能移动到下一行的邻接数字. 例如,给出如下三角形: [ [2], [3,4], [6,5,7], [4,1,8 ...
随机推荐
- Linux在目录中查找某个函数
1,在某个路径下查文件. 在/etc下查找“*.log”的文件 find /etc -name “*.log” 2,扩展,列出某个路径下所有文件,包括子目录. find /etc -name “*” ...
- HDU1004 BALLO0N
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Http协议访问DataSnap Rest 服务器
用TIDHttp访问DataSnap Rest服务器,在服务器采用了用户验证的情况下,客户端需要注意下面的细节,否则不能正常连接. 假如服务器有如下的用户验证: procedure TSC.DSAut ...
- 【转】局域网内访问VS2012 调试的IIS Express web服务器
1.修改发布项目的web属性 2.在我的文档中打开IISExpress\config\applicationhost.config 加上下面的一句 3.重启调试 转自:http://blog.chin ...
- innerHTML,innertext ,textcontent,write()
innerhtml属于对象的一个属性,一般用于向已经存在的标签中写入内容,或者读取标签的内容. innertext属于对象的一个属性,一般只能用于写入内容,或者读取内容,不能读取dom 中的标签,且只 ...
- VG.net矢量图和矢量动画开发平台拓扑图软件免费下载
VG.net拓扑图软件是一个基于.net平台的矢量图开发工具,可广泛应用于包括:电力.军工.煤炭.化工.科研.能源等各种监控软件.工作流设计器.电力.化工.煤炭.工控组态软件.仿真.地理信息系统.工作 ...
- JSP应用程序(自定义错误页面)
一.编写 1.index.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> &l ...
- [转]Neural Networks, Manifolds, and Topology
colah's blog Blog About Contact Neural Networks, Manifolds, and Topology Posted on April 6, 2014 top ...
- 数据库使用fmdb
#import "SQLdataManger.h" #import "FMDatabaseAdditions.h" static SQLdataManger * ...
- Android Listview 性能优化
首先我一般使用的适配器是BaseAdapter,其中有两个方法最主要,分别是: getCount,getView, 在对Listview 进行优化的时候,首先使用 convertview 和viewH ...