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. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  2. “System.Data.Entity.Internal.AppConfig"的类型初始值设定项引发异常。{转}

    <connectionStrings> <add name="ConnectionStringName" providerName="System.Da ...

  3. c++11 内存模型解读

    c++11 内存模型解读 关于乱序 说到内存模型,首先需要明确一个普遍存在,但却未必人人都注意到的事实:程序通常并不是总按着照源码中的顺序一一执行,此谓之乱序,乱序产生的原因可能有好几种: 编译器出于 ...

  4. Oracle自带的用户

    Oracle安装完毕创建数据库实例的时候,会自动生成三个用户sys,system,scott. sys用户是超级管理员,具有最高权限,充当sysdba角色,可以执行create database,默认 ...

  5. [Qt] 界面美化 [2013-06-17更新](转载)

    - 经验总结 1. 可用对话框(QDialog)模拟类似Android中toast的效果.     - 设置程序界面风格 在main函数中QApplication::setStyle("wi ...

  6. POJ 2226

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7557   Accepted: 2791 Desc ...

  7. 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)

    什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...

  8. win32空项目创建窗体

    #include "stdafx.h" //窗口过程函数(系统自动调用,即回调函数)LRESULT WINAPI MsgProc(HWND hWnd,UINT msg,WPARAM ...

  9. iOS开发之都兴忱小结

    1.NSArray/NSDictionary ------> strong temp和self.arr是同一地址. 2.NSArray/NSDictionary ------->copy ...

  10. 华为上机:Tom的生日礼物

    Tom的生日礼物 描述: 四月一日快到了,Tom想了个愚人的好办法——送礼物.嘿嘿,不要想的太好,这礼物可没那么简单,Tom为了愚人,准备了一堆盒子,其中有一个盒子里面装了礼物.盒子里面可以再放零个或 ...