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的更多相关文章

  1. pascal+sublime搭建Pascal学习环境

    一.fpc安装 1. 下载:http://www.freepascal.org/down/i386/win32.var(或者:http://download.csdn.net/detail/wenph ...

  2. leetcode—pascal triangle

    1.题目描述 Given numRows, generate the first numRows of Pascal's triangle.   For example, given numRows ...

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

  4. [leetcode] 2. Pascal's Triangle II

    我是按难度往下刷的,第二道是帕斯卡三角形二.简单易懂,题目如下: Given an index k, return the kth row of the Pascal's triangle. For ...

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

  6. 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 [ ...

  7. leetcode 【 Pascal's Triangle 】python 实现

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...

  8. Leetcode_119_Pascal's Triangle II

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41851069 Given an index k, retu ...

  9. 三角形(Triangle)

    三角形(Triangle) 问题 给出一个三角形,找出从顶部至底部的最小路径和.每一步你只能移动到下一行的邻接数字. 例如,给出如下三角形: [ [2], [3,4], [6,5,7], [4,1,8 ...

随机推荐

  1. 滤镜与CSS3效果

    -webkit-filter是css3的一个属性,Webkit率先支持了这几个功能,感觉效果很不错.一起学习一下filter这个属性吧. 现在规范中支持的效果有: grayscale 灰度       ...

  2. 【转】nginx+tomcat+memcached (msm)实现 session同步复制

    出现session不同步时,请放到content.xml中,实际验证有效: tomcat + memcached + nginx 实现session共享 这里重点强调如何实现linux服务器上 服务器 ...

  3. JS日期的获取与加减

    1)获取当前日期: var today = new Date(); 2)设定某个日期: var d = new Date("2015/1/08".replace(/-/g,&quo ...

  4. iOS webView 远程html加载本地资源

    昨天,一个朋友让我帮他在IOS上弄这样一件事情: webView 调用远程URL,并且让远程的web 通过自定义标签能实现内嵌本地的图片.js 或音频等. 比如:在服务器端 的html文件中 这样写到 ...

  5. ajax对象属性withCredentials

    默认情况下,ajax跨源请求不提供凭据(cookie.HTTP认证及客户端SSL证明等).通过将设置ajax的withCredentials属性设置为true,可以指定某个请求应该发送凭据.如果服务器 ...

  6. android技巧(四)数据库跨版本升级写法

    当项目中接手的需求需要在就前数据库数据表做出修改时,不得不面对数据库表结构的更新问题.一般的sqlite数据库更新修改数据库版本号时都会自动调用SqliteOptenHelper及其子类中的onUpg ...

  7. cassandra CQL 3.0 怎样实现 dynamic column;

    1. cassandra有一个好的特点是列之间可以按照column key进行排序:这样当rowkey确定以后,对于同一个“行”的范围(range query)查找是很方便的:官方说法,每一个“行”( ...

  8. 详解<a>标签

    相信对于学前端的人而言<a>标签并不陌生,我们先把他所有的属性列出来 一.主要属性 一般来说,a标签有着四种状态,分别是link,hover,active,visited,接下来我会仔细讲 ...

  9. 日常笔记 ---- 图形学-Frenel函数材质球实现方式

    图形学-Frenel函数材质球实现方式   调个材质 大概公式 自发光= 自定义边光颜色* ((1-法线与视角方向点乘)的 自定义幂次方 ) 这个是比较简单方法   模型的法线与视角方向 角度越大 表 ...

  10. PCL中point cloud的数据类型

    出处: http://wiki.ros.org/pcl/Overview 1.数据类型 1.1 ROS中point cloud数据类型 sensor mesgs::PointCloud sensor ...