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. HDU 3397 Sequence operation

    题目:下列操作 Change operations:0 a b change all characters into '0's in [a , b]1 a b change all character ...

  2. ASP.NET Web API 入门示例详解

    REST服务已经成为最新的服务端开发趋势,ASP.NET Web API即为.NET平台的一种轻量级REST架构. ASP.NET Web API直接借鉴了ASP.NET MVC的设计,两者具有非常类 ...

  3. [安卓]应用程序资源(App Resources)

    谷歌推荐我们,在开发安卓系统应用程序的时候,要把资源从代码中分离出来,这样便于我们单独维护它们.采取分离的资源设计,我们还可以提供可选资源,支持特定的设备配置譬如不同的语言或屏幕尺寸,随着越来越多的A ...

  4. Notes of learning AutoLayout

    在XCode5中,如果我们添加一个Button或者Label,或者其他的什么标准View,而不设置任何constraints,IB会自动生成constraints,而这些constraints是fix ...

  5. js限制文本框只能输入整数或者带小数点[转]

    这篇文章是关于js限制文本框只能输入整数或者带小数点的内容,以下就是该内容的详细介绍. 做表单验证的时候是否会碰到验证某个输入框内只能填写数字呢,仅允许输入整数数字或者带小数点的数字.下面这段代码也许 ...

  6. REDIS key notification

    Commands Clients Documentation Community Download Support License Join us in London October 19th for ...

  7. HDU 2277 Change the ball

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2277 Change the ball Time Limit: 2000/1000 MS (Java/O ...

  8. bll编译错误

    如果在项目中 ,bll有函数,却引用报错 原因很可能是因为bll在生成程序集的时候,没有生成好.其中有错误 解决办法. 1.将bll,web,dal重新生成 2.注意bll的生成,该添加的添加,该排除 ...

  9. VISIBLE、INVISIBLE、GONE的区别

    VISIBLE:设置控件可见 INVISIBLE:设置控件不可见 GONE:设置控件隐藏 而INVISIBLE和GONE的主要区别是:当控件visibility属性为INVISIBLE时,界面保留了v ...

  10. linux 中printf的使用

    linux 中printf的使用printf "helloworld\n"printf 中换行必须加上\n printf '%d %s\n' 1 "abc" c ...