[LeetCode 118] - 杨辉三角形(Pascal's Triangle)
问题
给出变量numRows,生成杨辉三角形的前numRows行。
例如,给出numRows=5,返回:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
初始思路
基本算法和 杨辉三角形II(Pascal's Triangle II) 的基本一致。每算完一行的值将这些值拷贝一份到vector中即可。代码如下:
class Solution {
public:
std::vector<std::vector<int> > generate(int numRows)
{
std::vector<std::vector<int> > result;
std::vector<int> columnInfo;
if(numRows == )
{
return result;
}
columnInfo.push_back();
result.push_back(columnInfo);
if(numRows == )
{
return result;
}
columnInfo.push_back();
for(int i = ; i < numRows; ++i)
{
for(int j = i; j > ; --j)
{
//第一列和最后一列永远为1,不需要进行处理
if(j != && j != i)
{
columnInfo[j] = columnInfo[j - ] + columnInfo[j];
}
}
result.push_back(columnInfo);
//下一行开始列数相应增加,且最后一列的数字肯定是1
columnInfo.push_back();
}
return result;
}
};
generate
[LeetCode 118] - 杨辉三角形(Pascal's Triangle)的更多相关文章
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 118. 119. Pascal's Triangle -- 杨辉三角形
118. Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- 【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(118) Pascal's Triangle
题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...
- 【一天一道LeetCode】#119. Pascal's Triangle II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- Leetcode No.119 Pascal's Triangle II(c++实现)
1. 题目 1.1 英文题目 Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's tria ...
- LeetCode OJ 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, ...
随机推荐
- CTime,Systemtime的比较还有转换成日期格式。
vc为我们提供了两种日期型的变量. 一种是CTime.他的缺点就是年份只支持到2038年,以后的日期就不支持啦,如果你的项目有20-30年的寿命,你就选择使用SYSTEMTIME.这个时间函数来进行比 ...
- [Angular 2] Managing State in RxJS with StartWith and Scan
The scan operator in RxJS is the main key to managing values and states in your stream. Scan behaves ...
- C文件读写函数介绍(转)
1.fopen() fopen的原型是:FILE *fopen(const char *filename,const char *mode),fopen实现三个功能:为使用而打开一个流,把一个文件和此 ...
- [转] PostgreSQL学习手册(函数和操作符)
一.逻辑操作符: 常用的逻辑操作符有:AND.OR和NOT.其语义与其它编程语言中的逻辑操作符完全相同. 二.比较操作符: 下面是PostgreSQL中提供的比较操作符列表: 操作符 描述 < ...
- Sql 语句添加字段、修改字段类型、默认值语法
Sql 语句添加字段 ,) not null --修改类型 alter Table bbs ) Sql 语句修改默认值 alter table 表名 drop constraint 约束名字 --删除 ...
- Java基础知识强化21:Java中length、length()、size()区别
1.java中的length属性是针对数组说的,比如说你声明了一个数组,想知道这个数组的长度则用到了length这个属性.2.java中的length()方法是针对字符串String说的,如果想看这个 ...
- linux定时任务1-crontab命令
简单测试例子: 添加定时任务前,注意查看crond服务是否已经启动,如果未启动,则用命令service crond start命令启动. 注意给脚本添加可执行权限. [root@rheltest1 ~ ...
- 06-自定义Attribute标记案例
自定义Attribute: 1)Attribute都从System. Attribute类继承,类名一般以Attribute结尾 2) 标记类的用途—AttributeUsage标记(标记的标记):A ...
- FastReport 动态修改连接字符串
代码如下: Report rp = new Report(); rp.Load(@"Print\aa.frx"); rp.Dictionary.Connections[0].Con ...
- Java分页类 Page
import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Iterator; ...