118.

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) {
vector<vector<int>> ans;
if(numRows < )
return ans;
vector<int> pre, cur;
pre.push_back();
ans.push_back(pre);
for(int i = ; i < numRows; i++)
{
cur.push_back();
for(int j = ; j < pre.size()-; j++)
{
cur.push_back(pre[j]+pre[j+]);
}
cur.push_back();
ans.push_back(cur);
pre = cur;
cur.clear();
}
return ans;
}
};

119.

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+, );
ans[] = ;
for(int i = ; i <= rowIndex; i++)
{
ans[i] = ;
for(int j = i-; j > ; j--)
{
ans[j] += ans[j-];
}
}
return ans;
}
};

只开一个数组,从后向前计算。

118. 119. Pascal's Triangle -- 杨辉三角形的更多相关文章

  1. 【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, ...

  2. 118/119. Pascal's Triangle/II

    原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...

  3. leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2

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

  4. leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2

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

  5. 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- ...

  6. LeetCode OJ 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, ...

  7. 118. Pascal's Triangle杨辉三角形(全部/一行)

    [抄题]: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

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

  9. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

随机推荐

  1. How To PLAY_SOUND in Oracle Forms

    Play_sound is used to play audio files in Oracle Forms, Play_Sound plays the sound object in the spe ...

  2. [原创]VB注册机独辟蹊径-----注册机也可以这样写

    近段时间接了个项目,是关于一个称重传感器的上位机系统,需要一机一码针对不同的客户机分别注册,第一次注册完后,下次打开后不必注册. 刚开始想用正规的注册机办法去完成,搜罗了半天,发现现在大部分的硬盘注册 ...

  3. 在Spring中使用脚本

    Spring支持3中不同的脚本语言(看来支持地还挺多的嘛):JRuby.Groovy和BeanShell. 这三个都是java社区的脚本语言(反正到目前为止我一个都没用过,可见我有多挫). JRuby ...

  4. Python基础学习笔记(七)常用元组内置函数

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-tuples.html 3. http://www.liaoxue ...

  5. 如何设置table中<tr>和<td>的高度

    //-----------------自定义表格table的行和列的宽和高----------------------// 先设置一个样式 如下: <style type="text/ ...

  6. C++大数类模板

    友情提示:使用该模板的注意了,在大数减法里有一个小错误,导致减法可能会出错 // 原来的写法,将t1.len错写成了len ] == && t1.len > ) { t1.len ...

  7. iOS - UIWebView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIWebView : UIView <NSCoding, UIScrol ...

  8. 百度之星复赛Astar Round3

    拍照 树状数组(SB了).求出静止状态下,每个点能看到多少个向右开的船c1[i],多少个向左开的船c2[i]. max{c1[i] + c2[j], (满足i <= j)  }即为答案.从后往前 ...

  9. 用Jquery获取select的value和text值

    $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 var checkText=$(&q ...

  10. 【Todo】蒙特卡洛(蒙特卡罗)树 & 卷积网络

    https://www.zhihu.com/question/41176911/answer/90066752 这里面有关于Deep Learning和蒙特卡洛树的一些内容 https://www.z ...