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] ]

C++

class Solution {
public:
vector<vector<int> > generate4(int numRows) {
vector<vector<int>> dp(numRows);
if (numRows < 1) return dp;
dp[0].push_back(1);
for (int i =1; i < numRows; i++){
dp[i].push_back(1);
for(int j = 1; j < i; j++)
dp[i].push_back(dp[i-1][j-1] + dp[i-1][j]);
dp[i].push_back(1);
}
return dp;
} vector<vector<int>> generate(int numRows){
vector<vector<int>> dp(numRows);
for(int i = 0; i < numRows; ++i){
dp[i].push_back(1);
for(int j = 1; j < i; ++j)
dp[i].push_back(dp[i-1][j-1] + dp[i-1][j]);
if (i>0)
dp[i].push_back(1);
}
return dp;
}
};

pascals-triangle leetcode C++的更多相关文章

  1. [Leetcode] pascals triangle ii 帕斯卡三角

    Given an index k, return the k th row of the Pascal's triangle. For example, given k = 3,Return[1,3, ...

  2. [LeetCode][Java]Triangle@LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. Triangle leetcode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  4. Triangle——LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. Pascal's Triangle leetcode

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  6. Triangle LeetCode |My solution

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. Pascal's Triangle leetcode java(杨辉三角)

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

  8. Triangle leetcode java

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  9. triangle leetcode C++

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  10. 决战Leetcode: easy part(1-50)

    本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...

随机推荐

  1. POJ题目 1003Hangover(叠放纸牌)

    POJ 1003 叠放纸牌 描述 您可以将多张纸牌悬在桌子上多远?如果您有一张卡,则可以创建一个最大长度为卡长的一半.(我们假设这些卡片必须垂直于桌子.)使用两张卡片,您可以使最上面的卡片悬垂在底部的 ...

  2. 【OI】C++STL 不定长数组 vector

    Vector 本来是向量的意思,只不过在用法上类似于一个不限长度的数组. 定义语法:vector<数据类型> 名称; 一.头文件:<vector> (bits/stdc++请忽 ...

  3. HCNP Routing&Switching之路由策略工具Route-Policy

    前文我们了解了路由过滤和路由过滤工具Filter-Policy使用相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15316188.html:今天我们来 ...

  4. nginx负载轮询

    下面是一个可以使用nginx负载轮询,如果有一台服务器连接不通,返404,500,502,503,504,会自动切换到下一台服务器 upstream www { server 111.111.111. ...

  5. C# 在PPT中添加数学公式

    本次内容介绍在C#程序中给PPT幻灯片添加Latex数学公式,添加公式前,首先需要在幻灯片中插入一个Shape形状,在形状的段落中通过方法Paragraphs.AddParagraphFromLate ...

  6. c++ 的学习 第二集函数的重载

    1. 规则 函数名相同参数个数不同.参数类型不同.参数顺序不同 2. 注意 返回值类型与函数重载无关 调用函数时,实参的隐式类型转换可能会产生二义性 返回值类型与函数重载无关 什么意思? 返回 ...

  7. 软件测试工程师简历要怎么写,才能让HR看到

    作为软件测试的从业者,面试或者被面试都是常有的事. 可是不管怎样,和简历有着理不清的关系,面试官要通过简历了解面试者的基本信息.过往经历等. 面试者希望通过简历把自己最好的一面体现给面试官,所以在这场 ...

  8. ThreadLocal基本使用和内存泄漏分析

    ThreadLocal基础部分 ThreadLoal的作用 保存线程的独立变量,即每个线程维护一份.这种变量在线程的生命周期内起作用,减少同一个线程内多个函数之间公共变量传递麻烦. 使用场景 需要给不 ...

  9. Windows10 IIS Web服务器安装配置

    前言: 对于.NET开发者而已,IIS Web托管服务器应该是十分的熟悉的.对于刚安装Windows10的系统的用户而已Internet Information Services(IIS)功能是默认关 ...

  10. virtualbox 桥接模式网络配置虚拟机之间通讯以及虚拟机联网

    一般来说桥接模式可以解决所有的网络问题 网卡选择 [root@HELLO network-scripts]# cat ifcfg-eth0 TYPE="Ethernet" PROX ...