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

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. WEB服务重要基础

    1.1用户访问房展基本流程 我们每天都会使用Web客户端上网浏览网页.最常见Web客户端就是Web浏览器,如通过的微软InternetExplorer(IE)以及技术人员偏爱的火狐浏览器.谷歌浏览器等 ...

  2. C#中 标识符“XXX”不符合 CLS

    标识符“XXX”不符合 CLS,意思是只要是不与外面有接口,比如在私有函数中操作,可是使用一些不符合cls的类型,但是如果是公共的,就必须要符合这个规范. 解决方法是,将这个类中的这些public类型 ...

  3. 上传项目到git

    …or create a new repository on the command line   echo "# test" >> README.md git ini ...

  4. ListView 中 的 分页

     Django Pagination 简单分页 当博客上发布的文章越来越多时,通常需要进行分页显示,以免所有的文章都堆积在一个页面,影响用户体验.Django 内置的 Pagination 能够帮助我 ...

  5. 【308】Python os.path 模块常用方法

    参考:Python os.path 模块 参考:python3中,os.path模块下常用的用法总结 01   abspath 返回一个目录的绝对路径. 02   basename 返回一个目录的基名 ...

  6. spring4-2-bean配置-7-Spring表达式语言SpEL

  7. VMware安装完后,没有虚拟网卡

    1 问题描述: 1.1 windows10首次安装VMware,或者非首次安装VMware时,安装后,没有出现如下图所示的虚拟网卡: 1.2 Xshell或者SecureCRT 或者editplus等 ...

  8. fastdfs 有用 新增tracker或storage

    FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务,如相 ...

  9. HTTPS加密那点事--轻松秒懂HTTPS非对称加密

    本文转载自微信公众号(苦逼的码农),原文地址: https://mp.weixin.qq.com/s/j-ss95ItMnWsZHLpUGBMkQ 用漫画的形式解释技术问题是不是有眼前一亮的感觉呢?以 ...

  10. ubuntu下搭建android开发环境之超顺畅模拟器

    如果说android系统的卡,像耳边蚊子让人抓狂,那么android模拟器的卡,那就像午睡时的苍蝇.大概就是一样的恶心~~ 那么,这样的问题对于开发者肯定忍无可忍,我也一样,虽然我还没有入门,但我也一 ...