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. 定制自己的Unity脚本模板

    有时候想给脚本添加符合自己编程习惯的内容,或是一些个性化信息.而作为一个多多少少有点强迫症的人,这种东西要加就得每个脚本都加上,不然看着多不爽! 于是就得每添加一个脚本就去修改一下,很麻烦. 但是,在 ...

  2. 使用XML文件定义菜单

    Android提供了两种创建菜单的方式,一种是在Java代码中创建,一种使用XML资源文件定义.上面的实例都是在Java代码中创建菜单,在Java代码中创建菜单存在如下不足. 在Java代码中定义菜单 ...

  3. HTML5 简介、浏览器支持、新元素

    什么是 HTML5? HTML5 是最新的 HTML 标准. HTML5 是专门为承载丰富的 web 内容而设计的,并且无需额外插件. HTML5 拥有新的语义.图形以及多媒体元素. HTML5 提供 ...

  4. easyUI tootip组件使用

    easyUI tootip组件使用: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  5. 我的音乐盒子(nodejs7 + koa2 + vue + vuex + vue-router)

    你们知道的,nodejs对jser来说,是个好东西,快快的,自从接触nodejs后,总想弄点东西. 这弄个啥了,一天打开百度音乐盒,哟,自己弄一个如何了,好啊好啊. 后台: nodejs 7 + ko ...

  6. JavaScript Window.document对象

    一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:    var a =docunment.getElementById(&qu ...

  7. make clean指令出现问题

    今天第一次使用make指令,没搞懂make clean就直接使用了,结果发现如下错误问题:make: *** No rule to make target 'clear'. 停止. 最后的找了半天有下 ...

  8. Java中boolean类型到底占用多少字节

    虽然 Java 虚拟机定义了 boolean 这种数据类型,但是只对它提供了非常有限的支持.在 Java 虚拟机中没有任何供 boolean 值专用的字节码指令,在 Java 语言之中涉及到 bool ...

  9. c#.net的网站出现“正在中止线程””异常的原因和解决方法

    出现“正在中止线程”异常通常都是由于以下三种原因导致引起,给出解决方案如下: 解决方案: 1.针对Response.End,调用 HttpContext.Current.ApplicationInst ...

  10. C#文本框允许使用ctrl+A

    C#文本框中默认是不允许使用全选的.可以通过以下事件完成: private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.C ...