[LeetCode] Pascal's Triangle 杨辉三角

时间:2022-02-07 17:15:00

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.

[LeetCode] Pascal's Triangle 杨辉三角
In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

杨辉三角是二项式系数的一种写法,如果熟悉杨辉三角的五个性质,那么很好生成,可参见我的上一篇博文Pascal's Triangle II。具体生成算是:每一行的首个和结尾一个数字都是1,从第三行开始,中间的每个数字都是上一行的左右两个数字之和。代码如下:

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res(numRows, vector<int>());
for (int i = ; i < numRows; ++i) {
res[i].resize(i + , );
for (int j = ; j < i; ++j) {
res[i][j] = res[i - ][j - ] + res[i - ][j];
}
}
return res;
}
};

类似题目:

Pascal's Triangle II

参考资料:

https://leetcode.com/problems/pascals-triangle/

https://leetcode.com/problems/pascals-triangle/discuss/38150/My-C%2B%2B-code-0ms

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Pascal's Triangle 杨辉三角的更多相关文章

  1. &lbrack;leetcode-118&rsqb;Pascal&&num;39&semi;s triangle 杨辉三角

    Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  2. LeetCode118&period; Pascal&&num;39&semi;s Triangle 杨辉三角

    题目 给定行数,生成对应的杨辉三角 思考 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2 对于第偶数行,个数也是偶数,对于奇数行,个 ...

  3. 【LeetCode每天一题】Pascal&&num;39&semi;s Triangle&lpar;杨辉三角&rpar;

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

  4. Pascal&&num;39&semi;s Triangle&lpar;杨辉三角&rpar;

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

  5. &lbrack;LeetCode&rsqb; 119&period; Pascal&&num;39&semi;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, ...

  6. LeetCode——Pascal&&num;39&semi;s Triangle

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

  7. 20190105-打印字母C&comma;H&comma;N&comma;口等图像和杨辉三角

    1. 打印字母C ****** * * * * ****** def print_c(n): print('*' * n) for i in range(n): print('* ') print(' ...

  8. &lbrack;LeetCode&rsqb; Pascal&&num;39&semi;s Triangle II 杨辉三角之二

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

  9. LeetCode 118&period; Pascal&&num;39&semi;s Triangle (杨辉三角)

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

随机推荐

  1. 如何在本地搭建IIS服务器

    http://jingyan.baidu.com/article/8ebacdf021313d49f65cd525.html

  2. 试听笔记:javascript入门精通

    一.数据类型 1.原始类型:number.string.boolean.null.undefined 2.对象类型:Object (Function.Array.Date...) P.类型隐式转换:' ...

  3. 用GSON解析Json格式数据

    GSON是谷歌提供的开源库,用来解析Json格式的数据,非常好用.如果要使用GSON的话,则要先下载gson-2.2.4.jar这个文件,如果是在Android项目中使用,则在Android项目的li ...

  4. Spring 之autowired

    Spring中autowired主要用于装配树形值,其关键类为BeanWrapperImpl,阅读代码发现其关键方法setPropertyValue有如下一段代码. PropertyHandler p ...

  5. HTML基础和表格

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  6. EBuild-API常见问题汇总

    问题1:用户访问被防火墙屏蔽检查处理流程 E-Build API(原IBE)是面向航空公司和代理人,以及第三方的航空预订服务产品.用户通过客户端,使用固定IP访问E-Build API服务器,航信网络 ...

  7. window下自己主动备份数据库成dmp格式的bat写法

    复制以下的命令到一个txt文本文档,然后改动相应的參数为自己须要的參数,一切完毕之后,将文件保存为bat格式. 这样每次须要备份的时候仅仅须要双击一下这个bat文件.其它的都不用你了,你仅仅须要静静的 ...

  8. HDU4734&lpar;数位dp&rpar;

    F(x) Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. 云储存第三方--阿里云OSS VS 又拍云USS

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p. ...

  10. Jmeter 相关资源

    官网:http://jmeter.apache.org/ 插件: https://jmeter-plugins.org