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. Hibernate 检索方式

    概述 •Hibernate 提供了以下几种检索对象的方式 –导航对象图检索方式:  根据已经加载的对象导航到其他对象 –OID 检索方式:  按照对象的 OID 来检索对象 –HQL 检索方式: 使用 ...

  2. Java 获取网络重定向文件的真实URL

    其实Java 使用HttpURLConnection下载的的时候,会自动下载重定向后的文件,但是我们无法获知目标文件的真实文件名,文件类型,用下面的方法可以得到真实的URL,下面是一个YOUKU视频的 ...

  3. UVA 10801 Dij最短路(改模板)

    题意:有n个电梯,目的地是第K层(起点是第0层),给出每个电梯的速度,以及每个电梯能到达的层数,如果中途需要换电梯的话,时间需要+60,求到达目的地的最短时间: 思路:Dij求最短路.如果是另一条路比 ...

  4. codeforces 446B(优先队列)

    题目链接:http://codeforces.com/problemset/problem/446/B #include<bits/stdc++.h> using namespace st ...

  5. IOS网络开发(一)

    1 简易的聊天工具 1.1 问题 Socket的英文原义是孔或者插座的意思,通常也称作套接字,用于描述IP地址和端口,是一个通信链的句柄,本案例使用第三方Socket编程框架AsyncSocket框架 ...

  6. D - Half of and a Half 大数

    D - Half of and a Half Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I ...

  7. SVN提交碰到的问题奇怪的问题

    svn提交遇到恶心的问题,可能是因为上次cleanup中断后,进入死循环了. 错误如下: 解决方法:清空svn的队列 1.下载sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc.d ...

  8. 转 未能加载类型 xxxx

    未能加载类型 分析器错误   说明:   在分析向此请求提供服务所需资源时出错.请检查下列特定分析错误详细信息并适当地修改源文件. 分析器错误信息:   未能加载类型“xxxxx”. 错误的 < ...

  9. 编写更好的jQuery代码的建议(share)

    留个备份! 原文链接: Mathew Carella   翻译: 伯乐在线- yanhaijing译文链接: http://blog.jobbole.com/52770/ 讨论jQuery和javas ...

  10. [NOIP2009] 靶形数独(搜索+剪枝)

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教, Z 博士拿出了他最近发明的 ...