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]
]
分析:此题其实就是表现出帕斯卡三角的特征:它是一个三角形矩阵,其顶端是 1,视为(row0).第1行(row1)(1&1)两个1,这两个1是由他们上头左右两数之和 (不在三角形内的数视为0).依此类推产生第2行(row2):0+1=1;1+1=2;1+0=1.第3行(row3):0+1=1;1+2=3; 2+1=3;1+0=1. 循此法可以产生以下诸行。
思路:直接做,每次产生一个新行,开始每个都填充1然后再从元素1到i-1来进行更新。
代码如下:
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
for(auto i=0;i<numRows;++i)
{
res.push_back(vector<int>(i+1,1));
for(auto j=1; j<i; ++j) res[i][j] = res[i-1][j-1] + res[i-1][j];
}
return res;
}
};

  

												

Pascal's Triangle(帕斯卡三角)的更多相关文章

  1. LeetCode OJ:Pascal's Triangle(帕斯卡三角)

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

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

  3. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

  4. [Leetcode][JAVA] Pascal's Triangle I, II

    Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, give ...

  5. [LeetCode118]Pascal's Triangle

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

  6. [LeetCode119]Pascal's Triangle II

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...

  7. [leetcode] 3. Pascal's Triangle

    第三道还是帕斯卡三角,这个是要求正常输出,题目如下: Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  8. [leetcode] 2. Pascal's Triangle II

    我是按难度往下刷的,第二道是帕斯卡三角形二.简单易懂,题目如下: Given an index k, return the kth row of the Pascal's triangle. For ...

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

随机推荐

  1. matrix_last_acm_5

    password 123 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97960#problem/A 题意:给国王生日可能区间[a, ...

  2. vector内存分配

    vector,map 这些容器还是在堆上分配的内存,在析构时是释放空间 vector在提高性能可以先reserve在push_back() reserve:决定capacity,但没有真正的分配内存, ...

  3. while小问题

    while(!m_SMque.pop(data)); 看到这个有点忘了,如果pop返回false会一直执行pop,其实这个执行的是空语句,而while每次执行都需要判断条件,所以如果pop返回fals ...

  4. Oracle 时间处理(加减)

    一. 类似SQL SERVER中DateAdd select sysdate,add_months(sysdate,12) from dual;        --加1年 select sysdate ...

  5. WPF 详解模板

    在WPF中有三大模板 ControlTemplate,ItemsPanelTemplate,DataTemplate.其中ControlTemplate和 ItemsPanelTemplate是控件模 ...

  6. bootstrap学习记录(慕课网教程)

    1.当主标题下需要副标题时可在h中嵌套small<h1>nihao<small>a</samll></h1> 2.当某一段落需要突出显示时可添加lead ...

  7. CSS兼容问题大全

    1.chorme 最小字体的兼容性. 问题描述:ff和IE最小字体可设置为1px,可是chorme中文版最小字体是12px,小于12px的字体全部显示为12px.解决方案:chorme支持CSS3的, ...

  8. ZOJ3717 Balloon(2-SAT)

    一个很玄乎的问题,但听到2-SAT之后就豁然开朗了.题目的意思是这样的,给你n个点群,每个点群里面有两个点,你要在每个点群里面选一个点,以这些点做半径为r的圆,然后r会有一个最大值,问的就是怎么选这些 ...

  9. LA 3350

    The NASA Space Center, Houston, is less than 200 miles from San Antonio, Texas (the site of the ACM ...

  10. 真机模拟器.a文件编译报错