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. js 光标位置处理

    /** * 获取选中文字 * 返回selection,toString可拿到结果,selection含有起始光标位置信息等 **/ function getSelectText() { var tex ...

  2. 用php脚本比较MySQL两个数据库的结构差异

    define('DATABASE1', 'mysql://root:password@127.0.0.1/db1'); $dbi1 = new DbMysql; $dbi1->dbh = DAT ...

  3. jdom解析xml

    这次把代码整理了一下,打包发上来了,程序中需要用到的jar包也一起打在里面了,有兴趣的朋友可以解压后直接导入的eclipse里运行! xml是一种广为使用的可扩展标记语言,java中解析xml的方式有 ...

  4. 第五周 PSP 燃尽图 以及 进度条总结

    1.PSP DATE START-TIME END-TIME EVENT           DELTA TYPE 3.12 9.30 11.30 环境搭建 音乐30min QQ25min       ...

  5. jvm运行机制和volatile关键字详解

    参考https://www.cnblogs.com/dolphin0520/p/3920373.html JVM启动流程 1.java虚拟机启动的命令是通过java +xxx(类名,这个类中要有mai ...

  6. [cmd]如何设置 Windows 默认命令行窗口大小和缓冲区大小

    Windows 命令行 cmd 窗口系统默认的大小(80*40)对于现在的屏幕配置已经跟不上时代了,我们总是要把它改大些,而且缓冲区大小也想改得大大的.单纯的为当前的 Windows 命令行窗口修改显 ...

  7. mybatis学习六 parameterType 属性

    1. 在 XXXMapper.xml 中<select><delete>等标签的 parameterType 可以控制参数类型2. SqlSession 的 selectLis ...

  8. Python之内置函数一

    一:绝对值,abs i = abs(-123) print(i) # 打印结果 123 二:判断真假,all,与any 对于all # 每个元素都为真,才是True # 假,0,None," ...

  9. 2019.01.06 vijos lxhgww的奇思妙想(长链剖分)

    传送门 长链剖分模板题. 题意简述:允许O(nlogn)O(nlog_n)O(nlogn​)预处理,让你支持O(1)O(1)O(1)查找任意一个点的kkk级祖先. 思路:因为要O(1)O(1)O(1) ...

  10. 证明 U and V={0}时 dim(U+V)=dim(U)+dim(V)

    U And V={0} 证明 dim(U+V)=dim(U)+dim(V)设{u1,u2,...,uk} 是U的基,{v1,v2...,vr}是V的基,dim(U)=k ,dim(V)=r dim(U ...