leetcode解题报告(23):Pascal's Triangle
描述
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]
]
分析
其实就是杨辉三角,以前用队列写过。
为了和numRows相匹配,以变量i代表当前的行数,那么i-1才是当前行的下标。以j代表当前行的第i个元素(这个j是从下标1开始的)。
代码如下:
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<int>ele; //store elements of current line
vector<vector<int>>ret; //store all the lines and as a return variable
if(numRows == 0)return ret; //return an empty vector if numRows == 0
int j = 0; //initialize j
for(int i = 1; i <= numRows; ++i){ //note: i begins from 1,means the first line
ele.push_back(1); //push 1 before do any operators in a line
if(i >= 2){
j = 2;
while(j < i){
//because i starts with 1,so i - 1 is current line,while i - 2 is the line before current
//so as to j
ele.push_back(ret[i - 2][j - 2] + ret[i - 2][j - 1]);
++j;
}
ele.push_back(1); //the last number in a line is also 1
}
ret.push_back(ele);
ele.clear(); //each time we finish a line,clear this vector
}
return ret;
}
};
leetcode解题报告(23):Pascal's Triangle的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- leetcode解题报告(24):Pascal's TriangleII
描述 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [ ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode(119) Pascal's Triangle II
题目 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [ ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II
118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- [LeetCode&Python] Problem 118. Pascal's Triangle
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- [leetcode.com]算法题目 - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
随机推荐
- SAS学习笔记53 RTF单个字符标记设置
如何设置RTF中某一个字斜体而之后的字不斜体.下图中第一个P值都斜体并且加粗,第二个P值只有P进行了斜体和加粗
- NIO堆外内存与零拷贝
重点: 1.0拷贝需要系统支持. 普通内存模型: java线程内存 --> 操作系统内存 --> 硬盘 直接内存模型: java --> 操作系统内存 --> 硬盘 两者对比, ...
- Angular Material 学习笔记 Chips
依据 material guidelines, chips 可以用来做 filter https://material.io/design/components/chips.html#filter-c ...
- NEST指定id
1.默认以Id属性为Id,无Id属性则自动生成 2.可通过属性标签指定Id [ElasticsearchType(IdProperty = nameof(last_name))] public cla ...
- TODO-依赖注入与控制反转
交互框架之Actor与Listener的关系 https://www.cnblogs.com/mq0036/p/7473371.html
- Html CSS transform matrix3d 3D转场特效
Html CSS transform matrix3d 3D转场特效 透视矩阵 2n/(r-l) 0 (r+l)/(r-l) 0 0 2n/(t-b) (t+b)/(t-b) 0 0 0 (n+f)/ ...
- Fatal error:Call to undefined function mysqli_connect() in .php line 报错
这样的问题,多半是PHP配置问题. 修改php配置文件 1.在php(版本:php-7.2.7-Win32-VC15-x64)文件夹中一开始不会看到php.ini,而是php.ini-developm ...
- JAVA笔记整理(二),下载安装JDK
Windows平台 1.登录Oracle官方网站(http://www.oracle.com/index.html),找到下载 2.选择要下载的版本,点击JDK DOWNLOAD 3.下载文件,先勾选 ...
- JS在浏览器中输出各种三角形
直角三角形 <script type="text/javascript"> for(var i=1;i<=8;i++){ for(var j=1;j<=i; ...
- 【年度盘点】10个热门Python练习项目
这10个项目,约有270000人学习过. 今天给大家盘点一下实验楼最热门的10个Python练习项目.第二名是<200 行 Python 代码实现 2048>,第三名是<Python ...