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. mysql把之前表单进行拆分

    今天有个任务是需要把之前的历史数据做一个清理. 原历史数据 很多电话号码是放到了一起.所以需要新建一个联系方式表.然后进行增加 新建表格如下: 然后再进行查询数据. SELECT a.uid,subs ...

  2. PIE SDK算法的异步调用

    1.算法功能简介 异步方法一旦开始,方法调用就会立即返回,调用者就可以继续后续的操作.异步方法通常会在另外一个线程中,“真实”地执行着.整个过程,不会阻碍调用者的工作. PIE SDK支持算法功能的执 ...

  3. android点击桌面App图标activity启动流程

    1.点击桌面App图标,Launcher进程采用Binder IPC向system_server进程发起startActivity请求:2.system_server进程接收到请求后,向zygote进 ...

  4. CenctOS6 and CenctOS7 多种姿势解决忘记密码

    -----linux---- 忘记密码啦!!! 忘记密码教程!!! 教你们忘记密码(我原来密码就是123456,忘记是不可能的!假装忘记的样子 0.0) 现在我们忘记密码了!对忘记密码了.我忘记密码了 ...

  5. (转)网站速度优化技巧:Nginx设置js、css过期时间

    网站速度优化技巧:Nginx设置js.css过期时间 原文:http://www.webkaka.com/blog/archives/Nginx-set-the-expiration-time-for ...

  6. (转)创建DB2实例时出错,请大家帮忙解决

    创建DB2实例时出错,请大家帮忙解决 原文:http://bbs.chinaunix.net/thread-3601748-1-1.html 运行:$DB2DIR/instance/db2icrt   ...

  7. 【ExtJS】关于constructor、initComponent、beforeRender

    ExtJS提供的组件非常丰富,不过当原生的组件无法满足要求时,就需要扩展原生自定义组件了. initComponent 和 constructor 就是Extjs 提供用来实现继承和扩展的方式. 在E ...

  8. 触发Full GC的时机

    由于Full GC的耗时是Minor GC的十倍左右,所以Full GC的频率设计得比Minor GC低得多.现总结一下触发Full GC的情况. 在那些实现了CMS的比较新的虚拟机中,如果配置了-X ...

  9. JS 用正则表达式,验证密码包含数字和字母的方法

    必须包含至少一位数字和一位字母,脚本方法如下: function CheckPassWord(password) {//密码必须包含数字和字母 var str = password; if (str ...

  10. [转] EF cannot be tracked because another instance of this type with the same key is already being tracked

    本文转自:http://stackoverflow.com/questions/6033638/an-object-with-the-same-key-already-exists-in-the-ob ...