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

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. vue之slot,组件标签嵌套

    vue之slot,组件标签嵌套 插槽(Slot),在各种vue的ui插件中,经常见到的多个组件标签相互嵌套(如下)就是以此为基础的. <el-col > <el-checkbox & ...

  2. delphi 浮点 精度

    double 没有问题, Single有问题 '0.7' 0.69999999999999996 Single; // 4 byte real Double; // 8 byte real

  3. 服务器报警邮件发送到QQ邮箱,但是被系统拦截

    # 为啥发送到QQ邮箱呢?因为QQ邮箱可以和微信关联,第一时间收到消息 if 没有设置白名单,然后被拦截当做垃圾邮件了: 设置白名单就可以了,这样的状态特征是: 邮件在垃圾箱里面能找到 elif 还是 ...

  4. proxmox 安装ROS 备忘

    虚拟机设置:使用qemu64 CPU和vrtio网卡在家里测试性能最好.

  5. springboot mvc beetl模板 自定义错误的后缀问题

    @Component public class BeetlErrorViewResolver implements ErrorViewResolver { private static final M ...

  6. 微信公众号token 验证

    1. 首先给出测试项目的整体目录: 2. CoreServlet类: 当get请求的时候会执行get方法,post请求的时候会执行post方法,分别来处理不同的请求 package com.zjn.s ...

  7. tomcat https

    转自 http://11lingxian.iteye.com/blog/1491607 双向认证: 客户端向服务器发送消息,首先把消息用客户端证书加密然后连同时把客户端证书一起发送到服务器端, 服务器 ...

  8. 使用RampTexture来控制diffuse shading

    [RampTexture] RampTexture(渐变纹理),可以是1D/2D纹理. This allows you to accentuate the surface's colors to fa ...

  9. 知方可补不足~Sqlserver中的几把锁和.net中的事务级别 回到目录

    当数据表被事务锁定后,我们再进行select查询时,需要为with(锁选项)来查询信息,如果不加,select将会被阻塞,直到锁被释放,下面介绍几种SQL的锁选项 SQL的几把锁 NOLOCK(不加锁 ...

  10. 303. Range Sum Query 范围求和系列

    Immutable [抄题]: Given an integer array nums, find the sum of the elements between indices i and j (i ...