第三道还是帕斯卡三角,这个是要求正常输出,题目如下:

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

那么这个就很简单了,按照帕斯卡三角也是杨辉三角的定义来就可以了。我的代码如下:

vector<vector<int>> generate(int numRows)
{
vector<vector<int>> Pascal;
vector<int> tmp; if (numRows == 0)
{
return Pascal;
} tmp.push_back(1);
Pascal.push_back(tmp); for (int i = 1; i < numRows; i++)
{
tmp.clear(); tmp.push_back(1);
for (int j = 1; j < i; j++)
{
tmp.push_back(Pascal[i - 1][j - 1] + Pascal[i - 1][j]);
}
tmp.push_back(1);
Pascal.push_back(tmp);
} return Pascal;
}

这个跟上一题有一个区别是在这里认为帕斯卡三角的第0层应该返回[],而上一题中输入numRows==0时,返回的是[1],这个在最开始加一个判断就可以了没必要纠结。

[leetcode] 3. Pascal's Triangle的更多相关文章

  1. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  2. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

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

  3. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  4. LeetCode 119. 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, ...

  5. LeetCode 118. Pascal's Triangle (杨辉三角)

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

  6. [LeetCode] 119. Pascal's Triangle II 杨辉三角之二

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  7. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  8. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  9. 【leetcode】Pascal's Triangle

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

  10. LeetCode 119 Pascal's Triangle II

    Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...

随机推荐

  1. 回归问题中代价函数选择的概率解释(Probabilistic interpretation)

    在我们遇到回归问题时,例如前面提到的线性回归,我们总是选择最小而成作为代价函数,形式如下: 这个时候,我们可能就会有疑问了,我们为什么要这样来选择代价函数呢?一种解释是使我们的预测值和我们训练样本的真 ...

  2. CentOS7.6安装Git(IUS方式)

    官网下载地址:https://git-scm.com/download/linux 第一步:安装第三方存储库IUS curl https://setup.ius.io | sh 第二步:安装git y ...

  3. Spring Data MongoDB 三:基本文档查询(Query、BasicQuery

    一.简介 spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...

  4. Rhythmk 一步一步学 JAVA(9) JAVA 基础笔记[枚举,...]

    1.装箱就是值类型转换为object类型,拆箱相反:object转化为值类型 eg:Integer i=1; // 装箱 int j=i; // 拆箱 2.静态导入: eg: 导入: import s ...

  5. C#中如何判断线程当前所处的状态

    转自原文 在C#中如何判断线程当前所处的状态 在C#中,线程对象Thread使用ThreadState属性指示线程状态,它是带Flags特性的枚举类型对象.    ThreadState 为线程定义了 ...

  6. 思科ASA 基础学习

    ASA int e0/0 ip add 192.168.1.1 24nameif insidesecruity-leve 100 int e0/0/0ip add 192.168.2.1 24name ...

  7. SYN Flood 防范

    简介: SYN Flood 是 DoS( 拒绝服务攻击 )与 DDoS( 分布式拒绝服务攻击 )的方式之一,这是一种利用 TCP 协议缺陷,发送大量伪造 TCP 连接请求,从而使得服务器资源耗尽( C ...

  8. C#异步编程的一些认识

    1.使用委托类型的BeginXXX,EndXXX 2.使用事件 3.使用aysnc,await关键字,会自动切换回UI线程,启动方法的线程可以被重用,线程没有阻塞.内部其实是封闭了Task类的Cont ...

  9. 第2章地址Address(WCF全面解析3)

    WCF顾明思义,就是在Windows平台下解决通信(C,Communication)的基础框架(F,Foundation)问题. 终结点是WCF最为核心的对象,因为它承载了所有通信功能.服务通过相应的 ...

  10. nodejs中yield的用法?

    nodejs中yield的用法? https://www.zhihu.com/question/32752866?sort=created