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

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. Tkinter Entry

    Python - Tkinter输入(Entry): 用于接受用户Entry小窗口部件单行文本字符串.   用于接受用户Entry小窗口部件单行文本字符串. 如果你想显示多行文本可以编辑,那么你应该使 ...

  2. Windows C盘文件夹介绍及说明

    Documents and Settings是什么文件? 答案: 是系统用户设置文件夹,包括各个用户的文档.收藏夹.上网浏览信息.配置文件等. 补:这里面的东西不要随便删除,这保存着所有用户的文档和账 ...

  3. maven引入源码

    选中要添加的源码的项目右键-->debug--->debugs-configurations-->source-->java project

  4. REST 规范

    DRF之REST规范介绍及View请求流程分析 DRF之解析器组件及序列化组件 DRF - 序列化组件(GET/PUT/DELETE接口设计).视图优化组件 DRF之权限认证频率组件 DRF之注册器响 ...

  5. Netty生产级的心跳和重连机制

    今天研究的是,心跳和重连,虽然这次是大神写的代码,但是万变不离其宗,我们先回顾一下Netty应用心跳和重连的整个过程: 1)客户端连接服务端 2)在客户端的的ChannelPipeline中加入一个比 ...

  6. 聊聊flutter的UI布局

    UI布局多半是套路,熟悉套路的规则. Flutter的UI布局也有一套规则 center center可以让任何元素在屏幕中居中,既是水平居中又是垂直居中,如果想让元素从上而下排列要怎么办呢?那就得使 ...

  7. Django 使用体会

    最近急于赶项目,少有更新博文.如今项目大致不那么赶了,终于可以在晚上码字码文章,而不是码代码了. 从开始使用Django开发到现在, 也已经有大半年了.公司的项目也是逐步地加功能,加模块,一步步完善设 ...

  8. spring中bean 的属性id与name

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

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

  10. Python与Go冒泡排序

    #!/usr/bin/env python # -*- coding: utf-8 -*- # 冒泡排序法 def bubbling(array): # 时间复杂度:O(n^2) for i in r ...