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:迭代插入(未通过)

[              [               [                      [
[1], [1], [1], [1],
[1], [1,1], [1,1], [1,1]
[1], ==> [1,2], ==> [1,2,1], ==>...==> [1,2,1],
[1], [1,3], [1,3,3], [1,3,3,1],
[1] [1,4] [1,4,6], [1,4,6,4,1],
] [ [ [

可以推导出每一列的递推公式:

第一列从第一行开始:1

第二列从第二行开始:j / 1,  j∈[1,n)

第三列从第三行开始:[j*(j-1)] / [1*2], j∈[2,n)

...

第M列从第M行开始:[j*(j-1)*...*(j-m+2)] / [1*2*...*m-1], j∈[m,n)

但是程序未能实现,在输入numRows=8时,出现Runtime Error错误。

【★】解题思路2:

用f(n)(m)表示第n行第m个元素,n从1开始,1≤m≤n;

则f()(1)=f()(n)=1, f(n)(m)=f(n-1)(m-1) + f(n-1)(m);

核心代码只有四步:

①新建符合的空间;

②将首尾置为1;

③遍历除首尾外的元素空间,利用公式填充;

④将填充好的列表放入结果队列里;

 class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int> > rowslst; if (numRows == )
return rowslst; for (int i=; i<numRows; ++i) {
vector<int> row(i+);
row[] = row[i] = ; for(int j=; j<i; ++j) {
row[j] = rowslst[i-][j-] + rowslst[i-][j];
} rowslst.push_back(row);
} return rowslst;
}
};

附录:

杨辉三角与计算机

【Leetcode】【Easy】Pascal's Triangle的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【LeetCode每天一题】Pascal's Triangle(杨辉三角)

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

  5. 【leetcode刷题笔记】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, ...

  6. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

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

  8. LeetCode Array Easy 118. Pascal's Triangle

    Description Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. I ...

  9. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  10. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

随机推荐

  1. Permutation(构造+思维)

    A permutation p is an ordered group of numbers p1,   p2,   ...,   pn, consisting of ndistinct positi ...

  2. bzoj4034 树上操作 树链剖分+线段树

    题目传送门 题目大意: 有一棵点数为 N 的树,以点 1 为根,且树点有权.然后有 M 个操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有 ...

  3. SPOJ - COT2 离线路径统计

    题意:求\(u\)到\(v\)的最短路径的不同权值种类个数 树上莫队试水题,这一篇是上篇的弱化部分,但可测试以下结论的正确性 设\(S(u,v)\):\(u-v\)最短路径所覆盖的点集 \(S(u,v ...

  4. Sqlite CodeFirst的初级实现

    示例实体: using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnn ...

  5. 7.使用jenkins+marathon+docker完成自动化部署

    1.前置条件 1)Docker开启TCP端口,CloudBees Docker Build and Publish plugin插件会向目标主机docker生成docker镜像 开启docker ap ...

  6. Nginx + Lua搭建文件上传下载服务

    收录待用,修改转载已取得腾讯云授权 最新腾讯云技术公开课直播,提问腾讯W3C代表,如何从小白成为技术专家?点击了解活动详情 作者 | 庄进发 编辑 | 迷鹿 庄进发,信息安全部后台开发工程师,主要负责 ...

  7. Vue PDF文件预览vue-pdf

       最近做项目,遇到预览PDF这个功能,在网上找了找,大多推荐的是pdf.js,不过在Vue中还是想偷懒直接npm组件,最后找到了一个还不错的Vue-pdf 组件,GitHub地址:https:// ...

  8. Kafka使用多个分区时 consumer的Assign配置

    天天在给自己挖坑排坑... 因为要开多线程消费,所以分区加到了10,两个broker. Producer没有做特殊处理,所以是随机发到Partitions. 但是Consumer只做Subscribe ...

  9. Oracle RAC集群搭建(二)-基础环境配置

    01,创建用户,用户组 [root@rac1 ~]# groupadd -g 501 oinstall [root@rac1 ~]# groupadd -g 502 dba [root@rac1 ~] ...

  10. oracle基础知识(六)----spfile与pfile

    一, 认识参数文件      Oracle中的参数文件是一个包含一系列参数以及参数对应值的操作系统文件.它们是在数据库实例启动时候加载的,决定了数据库的物理 结构.内存.数据库的限制及系统大量的默认值 ...