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. win10 oracle11g安装问题:INS-13001环境不满足最低要求

    打开database文件夹,找到stage,然后cvu,找到cvu_prereq.xml文件,用记事本打开,增添下面内容: <OPERATING_SYSTEM RELEASE="6.2 ...

  2. HDU - 6188

    用vis表贪心异常方便 #include<bits/stdc++.h> #define rep(i,j,k) for(register int i=j;i<=k;i++) #defi ...

  3. Oracle的pipelined函数实现高性能大数据处理

    从Oracle 8开始,我们就可以从一个collection类型的数据集合中查询出数据,这个集合称之为"虚拟表".它的方法是"SELECT FROM TABLE(CAST ...

  4. Phyton pymssql连接数据库

    import pymssql # conn = pymssql.connect(server='longdabing',user='sa',password='sasa',database='long ...

  5. [V1-Team] WEDO创意论坛技术规格说明书

    WEDO 创意论坛技术规格说明书 0x0 文档版本 版本号 说明 v1.0 初步确定技术路线 附Github仓库:WEDO 0x1 技术说明 1. 前端框架   在主流的前端框架中,我们调研了Vue. ...

  6. 配intelliJ IDEA 过程

    1.安装svn 选项全选择,命令行执行要选择上2.安装java jdk 配jdk环境变量3.安装intelliJ IDEA 地址:http://idea.imsxm.com4.注册intelliJ I ...

  7. (转)超全整理!Linux性能分析工具汇总合集

    超全整理!Linux性能分析工具汇总合集 原文:http://rdc.hundsun.com/portal/article/731.html 出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望, ...

  8. vuex中filter的使用 && 快速判断一个数是否在一个数组中

    vue中filter的使用 computed: mapState({ items: state => state.items.filter(function (value, index, arr ...

  9. Oracle命令整理

    1 常用命令 常用命令 1 sqlplus  scott/tiger@192.168.47.10:1521/orcl      后面不要加: sqlplus  sys/oracle  as sysdb ...

  10. 网站加入QQ聊天链接

    有时候我们的网站需要加入客服聊天功能,实现方式各不相同同,对于流量不大的网站,可以加入qq聊天的链接,点击链接,会打开本地qq的聊天窗口, 和指定的人会话.实现方式很简单,就是一个<a>标 ...