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. wpf中UserControl的几种绑定方式

    我们经常会抽取一些可重用的控件,某个属性是否需要重用,直接决定了这个属性的绑定方式. 1.完全不可重用的控件 有一些与业务强相关的控件,它们的属性完全来自ViewModel,越是相对复杂的控件,越容易 ...

  2. ajaxsearch要点1

    ajaxsearch中必须将form的class定义为pagerForm,才能在foot中submit按钮得到值

  3. 更新新网卡驱动,修复win7雷凌网卡Ralink RT3290在电脑睡眠时和启动网卡时出现蓝屏netr28x.sys驱动文件错误

    更新新网卡驱动,修复win7雷凌网卡Ralink RT3290在电脑睡眠时和启动网卡时出现蓝屏netr28x.sys驱动文件错误 我的本本是win7,雷凌网卡Ralink RT3290   802.1 ...

  4. Makefile 开发环境全能管家

    变量的应用: CC=gcc RM=rm EXE=main.exe OBJS=目标 伪目标的应用: .PHONY:clean 自动变量的应用: $@:表示一个规则的目标 $^:表示的是规则中的所有的先决 ...

  5. RSS(Residual Sum of Squares)的自由度为什么是n-1呢

    [转载请注明出处]http://www.cnblogs.com/mashiqi 在回归问题中,偶尔我们会遇到求方差的估计的情况.举了例子,我们常常通过Gaussian分布${\cal N}(\mu , ...

  6. cocos2d-x 3.2 listview scorllview 等容器在小米华为等部分手机显示泛白解决

    感觉记不住,代码贴上以免以后难找 在proj.android\src\org\cocos2dx\cpp\AppActivity.java 中的 public class AppActivity ext ...

  7. xmind的第八天笔记

  8. jq 文字上下不间断滚动实例

    <div class="ruzhuright">                     <div class="rzcontent"> ...

  9. Json.net 忽略实体某些属性的序列化

    遇到了一个小问题有一个用户类,结构和数据库一模一样,里面包含用户密码,要向前台返回用户信息的json数据,但是不能输出密码这个字段.之前的做法是 重新又建了一个不包含这个字段的新类,然后深度复制,总感 ...

  10. Java常用正则表达式验证工具类RegexUtils.java

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexUtils{ /** * 正则表达式 ...