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

Subscribe to see which companies asked this question

0 0
0[1]0
0[1 1]0
0[1 2 1]0
0[1 3 3 1]0
0[1 4 6 4 1]

帕斯卡三角形,它的值 a[i][j] = a[i-1][j-1] + a[i-1][j]; 注意如果i-1<0,则a[i-1]=0,也就是假设三角形周边的元素都是0

程序中我们可以直接让边缘的数值为1

vector<vector<int>> generate(int numRows) {
vector<vector<int>> ret;
for (int i = ; i < numRows; ++i)
{
vector<int> row;
for (int j = ; j <= i; ++j)
{
if (j == || j == i)
row.push_back();
else
row.push_back(ret[i - ][j - ] + ret[i - ][j]);
}
ret.push_back(row);
}
return ret;
}

Pascal's Triangle leetcode的更多相关文章

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

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

  2. [LeetCode] 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. [LeetCode] Pascal's Triangle 杨辉三角

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

  4. LeetCode 118 Pascal's Triangle

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

  5. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  6. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

  7. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  8. LeetCode——Pascal's Triangle

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

  9. 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, ...

随机推荐

  1. JavaScript成员属性读取

    var obj = {}; 检索一个不存在的成员属性的值,将返回undefined; 可以使用||运算符来填充默认值: var status = obj.status||'inistatus' 从un ...

  2. hyperLink的定制

    在iReport中增加hyperLink,点击之后没有反应:需要重新写一遍net.sf.jasperreports.view.JRViewer; 修改其中的gotoHyperlink方法: case ...

  3. 小试 Ninja

    Ninja 是最近冒出来的一个 build system,它很像 make,然而效率更高,对大项目支持更好.当然我用 Ninja 和效率无关(我又没有那种有几百个中间目标的 C++ 项目要 build ...

  4. doubango介绍

    1.doubango官网:http://www.doubango.org/ 2.doubango是一个开源的VOIP基础平台, 并能用于嵌入式和桌面系统的开源框架,该框架使用ANSCI-C编写,具有很 ...

  5. C++ 头文件系列(forward_list)

    简介 forwrad_list字面意思为前向列表,但实际上它是一种单向列表,只能从单一方向遍历. 单向链表实现 forward_list内部是用单向列表实现的,并且设计该库的时候就是以近乎手写的单向链 ...

  6. linux - tar命令简单使用

    tar 新建一个tar文档 touch file1 touch file2 mkdir dir1 touch dir1/file3 # 普通tar文档 tar -cf tar-file.tar fil ...

  7. 自述创业史 | XMove动作感应系统(二)

    XMove是沙漠君和几个死党从2010年开始开发的一套人体动作捕捉系统,软硬件全部自行开发,投入了大量的精力,历经三年,发展四个版本.文章分上下篇,本文为下篇,前三代的故事在<光荣与梦想| XM ...

  8. JAVA中的char类型

    1.JAVA中,char占2字节,16位.可在存放汉字 2.char赋值 char a='a';  //任意单个字符,加单引号. char a='中';//任意单个中文字,加单引号. char a=1 ...

  9. 用keychain这个特点来保存设备唯一标识。

    由于IOS系统存储的数据都是在sandBox里面,一旦删除App,sandBox也不复存在.好在有一个例外,那就是keychain(钥匙串). 通常情况下,IOS系统用NSUserDefaults存储 ...

  10. Unity编程标准导引-2.2Unity中的基本概念

    2.2Unity中的基本概念 上述介绍提到了几个概念:游戏对象.场景.资源.相机,这个小节我们来深入了解,同时进行一些实践性操作.不过首先,我们需要大概了解一下Unity的工程文件夹. 2.2.1工程 ...