[LeetCode 题解]: 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]
] 产生杨辉三角,比较简单,注意几个边界条件:
class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<int> vi;
vector<vector<int> > ans;
int i,j;
vi.push_back();
ans.clear(); // 注意初始化
9 if(numRows<=0) return ans;
10 if(1==numRows)
11 {
12 ans.push_back(vi);
13 return ans;
14 }
15 if(2==numRows)
16 {
17 ans.push_back(vi);
18 vi.push_back(1);
19 ans.push_back(vi);
20 return ans;
21 } ans.push_back(vi);
vi.push_back();
ans.push_back(vi);
for(i=;i<numRows;i++)
{
vi.clear();
vi.push_back();
for(j=;j<i;j++)
{
vi.push_back(ans[i-][j-]+ans[i-][j]);
}
vi.push_back();
ans.push_back(vi);
}
return ans;
}
};
转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!
[LeetCode 题解]: Pascal's Triangle的更多相关文章
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 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 [1,3, ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- leetcode 【 Pascal's Triangle II 】python 实现
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
随机推荐
- django第一课大概了解
项目目录介绍: -------- manage.py : Django项目里面的工具,通过它可以调用django shell和数据库等. mysite/ 主要是项目配置的信息,通常一次生成后不需要其他 ...
- vue引入jQuery、bootstrap
vue引入jQuery.bootstrap 1.使用vue-cli构建的vue项目 2.npm安装jquery.bootstrap npm install jquery 3.修改build/webpa ...
- Arduino学习笔记A6(补充) - 在串口读取多个字符串,并且转换为数字数组
功能如题目. 在串口收到逗号分割的6串数字比如 100,200,45,4,87,99 然后在6个PWM端口3, 5, 6, 9, 10, 11输出对应PWM值 代码注释很详细了,就不再说明了. ARD ...
- ubuntu下永久修改DNS
通过修改: sudo vi /etc/resolvconf/resolv.conf.d/base(这个文件默认是空的) 在里面插入: nameserver 8.8.8.8 nameserver 8.8 ...
- tree的所有节点都勾选上或者取消勾选
还有一个功能,就是让tree的所有节点都勾选上或者取消勾选,在api中找了一下有一个方法: check target 选中指定节点. 那我们只能是选中根节点后,实现全选. getRoot none 获 ...
- Linux命令详解1--文件和目录管理之文件查找和比较
1. 文件查找 1.1 strings命令 ------- 在对象文件或二进制文件中查找可打印的字符串.字符串是4个或更多可打印的任意序列,以换行或空字符结束. strings命令对识别随机对象文件很 ...
- vue 项目搭建
vue init webpack-simple 工程名字<工程名字不能用中文> 简单部署 vue init webpack 工程名字<工程名字不能用中文> 完整部署
- ubuntu18 tensorflow faster_rcnn cpu训练自己数据集
(flappbird) luo@luo-ThinkPad-W540:tf-faster-rcnn$ ./experiments/scripts/train_faster_rcnn.sh 0 pasca ...
- 相机IMU融合四部曲(一):D-LG-EKF详细解读
相机IMU融合四部曲(一):D-LG-EKF详细解读 极品巧克力 前言 前两篇文章<Google Cardbord的九轴融合算法>,<Madgwick算法详细解读>,讨论的都是 ...
- adf错误
1>无法验证事务处理中的所有行 运行项目报错: javax.faces.el.EvaluationException: oracle.jbo.TxnValException: JBO-27023 ...