题目描述:

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

Example:

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

要完成的函数:

vector<vector<int>> generate(int numRows)

说明:

1、这道题目给定一个行数,要求返回具有给定行数的帕斯卡三角形,结果存储在二维vector中。

2、明白题意,这道题不难,每一行第 j 个元素的数值都是上一行第 j 个元素的数值+上一行第 j -1个元素的数值,最后再push_back一个1。

代码如下(附详解):

    vector<vector<int>> generate(int numRows)
{
vector<int>res1;//每一行的vector
vector<vector<int>>res;//存储最后结果的vector
int i;
if(numRows==0)//边界条件,返回一个空的二维vector
return res;
res1.push_back(1);//第一行的vector
while(numRows--)
{
res.push_back(res1);//把当前行的res1压入res中
i=res1.size()-1;
while(i>0)//从res1的后面开始处理,这样不会影响每一步的处理
{
res1[i]=res1[i-1]+res1[i];
i--;
}
res1.push_back(1);//最后再压入一个1
}
return res;
}

上述代码之所以要从res1的后面开始处理,是因为这样不会影响后续的计算处理。

比如res1=[1,2,1],按照上述做法,res1[2]先变成1+2=3,所以res1是[1,2,3],接着再res1[1]=2+1=3,所以res1是[1,3,3],最后再压入一个1,变成最后的[1,3,3,1]。

但如果我们从前面开始处理,res1[1]=1+2=3,所以res1是[1,3,1],接着res1[2]=3+1=4?这时候已经改变了res1中我们需要的原始数值,而从后面开始处理就不会。

上述代码实测3ms,beats 96.52% of cpp submissions。

leetcode-118-Pascal's Triangle(生成具有n行的帕斯卡三角形)的更多相关文章

  1. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  2. 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- ...

  3. LeetCode 118 Pascal's Triangle

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

  4. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

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

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

  6. leetcode 118 Pascal's Triangle ----- java

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

  7. Java [Leetcode 118]Pascal's Triangle

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

  8. Java for LeetCode 118 Pascal's Triangle

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

  9. Leetcode 118 Pascal's Triangle 数论递推

    杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...

随机推荐

  1. Reveal CocoaPods的使用

    Reveal是配合开发者编辑各种用户界面参数一款工具,运行界面如下,模拟器和真机都支持. Reveal使用时中不需要添加其他代码,只需要ios工程加载Reveal.framework,如果是真机需要确 ...

  2. qt的exe文件查找依赖的dll

    用qtcreater编译完工程生成的exe文件往往会依赖dll文件.如何一次定位exe文件所以依赖的所有dll文件呢,今天发现了软件叫hap-depends. 截图如下: 用这个软件打开exe文件就会 ...

  3. Java多线程—JUC原子类

    根据修改的数据类型,可以将JUC包中的原子操作类可以分为4类. 1. 基本类型: AtomicInteger, AtomicLong, AtomicBoolean ;2. 数组类型: AtomicIn ...

  4. Linux cloc

    一.简介 cloc是一个基于perl的.十分好用的代码统计工具,它所支持的语言还算十分丰富.不过,还是有很多用的较少的语言是不支持的.   二.安装配置 1)官网安装教程 http://cloc.so ...

  5. scala开发环境

    1. Intellij IDEA Scala开发环境搭建 Intellij IDEA 15.0.3 默认配置里面没有Scala插件,需要手动安装,在Intellij IDEA 15.0.3 第一次运行 ...

  6. code1169 传纸条

    来自:http://www.cnblogs.com/DSChan/p/4862019.html 题目说找来回两条不相交路径,其实也可以等价为从(1,1)到(n,m)的两条不相交路径. 如果是只找一条, ...

  7. SpringMVC 2.5.6 +Hibernate 3.2.0

    spring MVC配置详解 现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时 ...

  8. 并查集 - 1611 The Suspects

    题目地址: http://poj.org/problem?id=1611 分析: - 数据结构 - parent[x] 表示 x 元素的父节点位置. - rank[x] 记录x的子链的长度, 以便在合 ...

  9. Qt的QPixmap半透明

    QPixmap pix1(":/PixmapTest/Resources/Chrysanthemum.jpg"); QPixmap temp(pix1.size()); temp. ...

  10. tomcat 无法clean 的bug

    如果你打开类似这种的文件夹了,那恭喜你,你无法正常clean E:\e\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0 请关 ...