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]
]
 class Solution {
public:
vector<vector<int> > generate(int numRows) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if( == numRows){
vector<vector<int> > result;
return result;
} vector<vector<int> > result(numRows); vector<int> first(,);
result[] = first;
for(int i=;i<numRows;i++){
result[i] = nextTriangle(result[i-]);
} return result;
} vector<int> nextTriangle(vector<int> a){
int k = a.size();
vector<int> next(k+);
for(int i=;i<k+;i++){
if(==i){
next[i] = a[i];
continue;
}
if(k==i){
next[i] = a[k-];
continue;
}
next[i] = a[i-]+a[i];
}
return next;
}
};

我的答案

思路:创建一个函数,使用上一行的vector去计算下一行的vector,然后反复调用即可。需要尤其注意输入的值为0时候的情况。

[leetcode.com]算法题目 - Pascal's Triangle的更多相关文章

  1. 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 [ ...

  2. LeetCode(118) Pascal's Triangle

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

  3. LeetCode Array Easy 119. Pascal's Triangle II

    Description Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's tria ...

  4. LeetCode Array Easy 118. Pascal's Triangle

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

  5. 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II

    118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  6. [LeetCode&Python] Problem 118. Pascal's Triangle

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

  7. LeetCode算法题-Pascal's Triangle II(Java实现)

    这是悦乐书的第171次更新,第173篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第30题(顺位题号是119).给定非负索引k,其中k≤33,返回Pascal三角形的第k ...

  8. LeetCode算法题-Pascal's Triangle(Java实现)

    这是悦乐书的第170次更新,第172篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第29题(顺位题号是118).给定非负整数numRows,生成Pascal三角形的第一个 ...

  9. [leetcode.com]算法题目 - Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

随机推荐

  1. JDK8集合类源码解析 - ArrayList

    ArrayList主要要注意以下几点: 1构造方法 2添加add(E e) 3 获取 get(int index) 4 删除 remove(int index)    ,   remove(Objec ...

  2. centOS 6.5下升级mysql,从5.1升级到5.6

    转载:https://www.cnblogs.com/vickygu2007/p/5066409.html #mysqldump -uroot -p --all-databases > data ...

  3. Alpha 冲刺 (1/10)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作,对多个目标检测及文字识别模型进行评估.实验,选取较 ...

  4. syslog、日志服务器安装、卸载详解、如何安装和卸载EventLog Analyzer

  5. 存储引擎中MYIASM是什么意思

  6. ACM-ICPC 2018 徐州赛区网络预赛 J Maze Designer(最大生成树,倍增lca)

    https://nanti.jisuanke.com/t/31462 要求在一个矩形中任意选两个点都有唯一的通路,所以不会建多余的墙. 要求满足上述情况下,建墙的费用最小.理解题意后容易想到首先假设全 ...

  7. STL六大组件

    1.容器 顺序容器.关联容器 2.算法 各种常用算法,sort.search.copy…… 3.迭代器 用来索引容器中元素,是容器与算法之间的胶合剂 4.仿函数(另名函数对象) 仿函数就是让一个类的使 ...

  8. Java,数据库中的数据导入到Excel

    private static void executeMethod(JobExecutionContext arg0) throws Exception{ try { TContrastService ...

  9. HDU 5212 Code (莫比乌斯反演)

    题意:给定上一个数组,求 析: 其中,f(d)表示的是gcd==d的个数,然后用莫比乌斯反演即可求得,len[i]表示能整队 i 的个数,可以线性筛选得到, 代码如下: #pragma comment ...

  10. AngularJS的select设置默认值

    AngularJS的select设置默认值 在使用Angular时候使用select标签时会遇到绑定数据指定默认显示值可这样实现 <!DOCTYPE html> <html ng-a ...