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. Asp.net 导入Excel(服务器不带Office)

    #region 把excel文件转换为DataSet. /// <summary> /// 把excel文件转换为DataSet. /// </summary> /// < ...

  2. html5.js

    可以让IE8等不支持Html5的浏览器,支持Html5元素,比如<header> <footer> <section>等标签 /* HTML5 Shiv v3.7. ...

  3. .NET设计模式(17):命令模式(Command Pattern)(转)

    概述 在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”.但在某些场合,比如要对行为进行“记录.撤销/重做.事务”等处理,这种无法抵御变化的紧耦合是不合适的.在这种情况下,如何将“行为 ...

  4. .NET设计模式(8):适配器模式(Adapter Pattern)(转)

    概述 在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?如何利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化,而又不引入额外的复杂度?这就 ...

  5. Linux安装Flash player

    在线自动更新:Flash Player Update(需java) ===================== 下载得到install_flash_player_11_linux.x86_64.tar ...

  6. uitableviewcell 和 uibutton

    如果cell上面只有一个button  可以设置button.tag=IndexPath.Row;得到当前点击的行数,设置button属性的时候,可以设置一个全局的button来记住当前点击的butt ...

  7. 驱动笔记 - IO端口和IO内存

    访问IO端口 (#include <asm/io.h>) 设备资源struct resource{ resource_size_t start; //资源起始物理地址 resource_s ...

  8. 删除提示 FOREIGN KEY 约束引用”

    有时想删除某个表时,提示“无法删除对象 'Orders',因为该对象正由一个 FOREIGN KEY 约束引用”,原因很简单不要急躁,它被其它表的外键引用了,所以无法删除,在此只需先找到哪些表的外键引 ...

  9. POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)

    思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...

  10. POJ 1573

    #include<iostream> #include<stdio.h> #define MAXN 15 using namespace std; char _m[MAXN][ ...