一. 题目描写叙述

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]
]

二. 题目分析

关于帕斯卡三角形的定义,可參考:http://baike.baidu.com/link?url=qk_-urYQnO4v6v3P4BuMtCa0tMNUqJUk4lmbkb1aqbqikBU-ndiMlTF20fq2QUjTTFTeTohZ72KFxgBnz4sJha

将该三角形的左边对齐。就行发现,tri[i][j] = tri[i-1][j-1] + tri[i-1][j]。因此这里的代码并没有什么技巧。

三. 演示样例代码

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
if (numRows <= 0)
return result;
else
{
vector<int> temp(1, 1);
result.push_back(temp);
if (numRows == 1) return result;
temp.push_back(1);
result.push_back(temp);
if (numRows == 2) return result; for (int i = 2; i < numRows; ++i)
{
vector<int> temp(i + 1, 1);
for (int j = 1; j < i; ++j)
temp[j] = result[i - 1][j - 1] + result[i - 1][j];
result.push_back(temp);
}
}
return result;
}
};

leetcode笔记:Pascal&#39;s Triangle的更多相关文章

  1. 【Leetcode】Pascal&#39;s Triangle II

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

  2. leetcode:Pascal&#39;s Triangle

    一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数 ...

  3. LeetCode——Pascal&#39;s Triangle

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

  4. 【leetcode】118. Pascal&#39;s Triangle

    @requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...

  5. LeetCode——Pascal&#39;s Triangle II

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

  6. leetcode - Pascal&#39;s Triangle

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

  7. [LeetCode]Pascal&#39;s Triangle II

    题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...

  8. Pascal&#39;s Triangle I,II

    题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...

  9. Pascal&#39;s Triangle II

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

随机推荐

  1. nginx报错502

    http请求流程:一般情况下,提交动态请求的时候,nginx会直接把 请求转交给php-fpm,而php-fpm再分配php-cgi进程来处理相关的请求,之后再依次返回,最后由nginx把结果反馈给客 ...

  2. BZOJ 3566 [SHOI2014]概率充电器 ——期望DP

    期望DP. 补集转化,考虑不能被点亮的情况, 然后就是三种情况,自己不能亮,父亲不能点亮它,儿子不能点亮它. 第一次计算比较容易,第二次计算的时候需要出去第一次的影响,因为一条线只能传导一次 #inc ...

  3. 【CCF】交通规划 Dijstra变形 优先级队列重载

    [题意] 给定一个无向图,求这个图满足所有点到顶点的最短路径不变的最小生成树 [AC] 注意双向边要开2*maxm 注意优先级队列 参考https://www.cnblogs.com/cielosun ...

  4. HDU 5015 233 Matrix(网络赛1009) 矩阵快速幂

    先贴四份矩阵快速幂的模板:http://www.cnblogs.com/shangyu/p/3620803.html http://www.cppblog.com/acronix/archive/20 ...

  5. Python入门--6--今天抄袭人家一篇日志--numpy这个

    Numpy NumPy的主要对象是同种元素的多维数组. 这是一个所有元素都是同一类型.通过一个正整数元祖索引的元素表格(通常元素都是数字) 在Numpy中维度(dimensions)叫做:轴 轴的个数 ...

  6. Jsp中路径问题

    ${pageContext.request.contextPath}”的作用是取出部署的应用程序名,这样不管如何部署,所用路径都是正确的. <!--使用绝对路径的方式引入CSS文件-->& ...

  7. Python中%r与%s的区别

    %r是rper()方法处理的对象 %s是str()方法处理的对象 其实有些情况下,两者处理的结果是一样的,比如说处理数据类型为int型对象: 例如1: print ('I am %d year old ...

  8. Lighttpd 服务器的安装

    https://www.cnblogs.com/rongfengliang/articles/3503228.html

  9. Struts2牛逼的拦截器,卧槽这才是最牛的核心!

    struts 拦截器 一 拦截器简介及简单的拦截器实例 Struts2拦截器是在访问某个Action或者Action的某个方法,在字段前或者之后实施拦截,并且Struts2拦截器是可以插拔的,拦截器是 ...

  10. Codeforces Gym 100286J Javanese Cryptoanalysis 傻逼暴力

    原题地址:http://codeforces.com/gym/100286/attachments/download/2013/20082009-acmicpc-northeastern-europe ...