LeetCode 118. Pascal's Triangle
2016-07-17 16:11
225 查看
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
Pascal三角的规则是:每一个数等于头上左右两个数之和。
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<int> v;
vector<vector<int>> res;
if(numRows == 0) return res;
v.push_back(1);
res.push_back(v);
if(numRows == 1) return res;
v.push_back(1);
res.push_back(v);
if(numRows == 2) return res;
v.clear();
int i;
for(i = 3; i <= numRows; i ++){
v.push_back(1);
int j;
for(j = 1; j < i - 1; j ++){
v.push_back(res[i - 2][j - 1] + res[i - 2][j]);
}
v.push_back(1);
res.push_back(v);
v.clear();
}
return res;
}
};
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
Pascal三角的规则是:每一个数等于头上左右两个数之和。
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<int> v;
vector<vector<int>> res;
if(numRows == 0) return res;
v.push_back(1);
res.push_back(v);
if(numRows == 1) return res;
v.push_back(1);
res.push_back(v);
if(numRows == 2) return res;
v.clear();
int i;
for(i = 3; i <= numRows; i ++){
v.push_back(1);
int j;
for(j = 1; j < i - 1; j ++){
v.push_back(res[i - 2][j - 1] + res[i - 2][j]);
}
v.push_back(1);
res.push_back(v);
v.clear();
}
return res;
}
};
相关文章推荐
- poj1061-青蛙的约会
- Base64
- 26 复杂链表的复制
- Java Arrays类进行数组排序
- 二叉树非递归求深度和节点个数
- 冒泡排序
- 【linux c】什么是野指针以及如何避免野指针_学习笔记_010
- Android自定义捕获Application全局异常
- 踩水
- 【转载】.NET/C#-uploadify视频文件or大文件上传
- IntelliJ IDEA Maven 安装配置
- 设计模式---结构类型---装饰
- 参考韩顺平老师的视频,用HTML写一个静态网页的邮箱
- addWidget的参数说明
- 【NOIP2016提高A组模拟7.17】锦标赛
- 天热无聊,用C语言函数指针实现的小函数^_^
- Eclipse之安装及汉化步骤
- Android开发——获取应用数据/缓存大小并清理缓存
- 内存按字节编址,地址区间为[90000H,CFFFFH],若用32K*8bit的存储器芯片构成该内存,需要__块
- C++ read函数读入int整形数据