leetcode-118-Pascal's Triangle(生成具有n行的帕斯卡三角形)
题目描述:
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
要完成的函数:
vector<vector<int>> generate(int numRows)
说明:
1、这道题目给定一个行数,要求返回具有给定行数的帕斯卡三角形,结果存储在二维vector中。
2、明白题意,这道题不难,每一行第 j 个元素的数值都是上一行第 j 个元素的数值+上一行第 j -1个元素的数值,最后再push_back一个1。
代码如下(附详解):
vector<vector<int>> generate(int numRows)
{
vector<int>res1;//每一行的vector
vector<vector<int>>res;//存储最后结果的vector
int i;
if(numRows==0)//边界条件,返回一个空的二维vector
return res;
res1.push_back(1);//第一行的vector
while(numRows--)
{
res.push_back(res1);//把当前行的res1压入res中
i=res1.size()-1;
while(i>0)//从res1的后面开始处理,这样不会影响每一步的处理
{
res1[i]=res1[i-1]+res1[i];
i--;
}
res1.push_back(1);//最后再压入一个1
}
return res;
}
上述代码之所以要从res1的后面开始处理,是因为这样不会影响后续的计算处理。
比如res1=[1,2,1],按照上述做法,res1[2]先变成1+2=3,所以res1是[1,2,3],接着再res1[1]=2+1=3,所以res1是[1,3,3],最后再压入一个1,变成最后的[1,3,3,1]。
但如果我们从前面开始处理,res1[1]=1+2=3,所以res1是[1,3,1],接着res1[2]=3+1=4?这时候已经改变了res1中我们需要的原始数值,而从后面开始处理就不会。
上述代码实测3ms,beats 96.52% of cpp submissions。
leetcode-118-Pascal's Triangle(生成具有n行的帕斯卡三角形)的更多相关文章
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- 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 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- leetcode 118 Pascal's Triangle ----- java
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Java [Leetcode 118]Pascal's Triangle
题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- Java for LeetCode 118 Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Leetcode 118 Pascal's Triangle 数论递推
杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...
随机推荐
- ubuntu eclipse opencv环境配置
项目——Properties——C/C++ Build——Settings 配置包含目录: GCC C++ Compiler ——Includes /usr/include /usr/local/ ...
- 在Qt中使用SQLite数据库
前言 SQLite(sql)是一款开源轻量级的数据库软件,不需要server,可以集成在其他软件中,非常适合嵌入式系统. Qt5以上版本可以直接使用SQLite(Qt自带驱动). 用法 1 准备 引入 ...
- Python 简单模块学习
1. openpyxl / xlrd / xlwt => 操作Excel 文件(xlsx格式) => xlrd + xlwt : 只能操作xls文件,分别负责读写, 暂时不讨论 => ...
- springMvc 核心配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- [GO]面向对象编程
对于面向对象编程的支GO语言的设计简洁而优雅,因为,GO语言没有沿袭传统面向对象中的诸多概念 比如继承(不支持继承,尽管匿名字段的内存布局和行为类似继承,但它并不是继承) 尽管GO语言没有封装.继承. ...
- redis 通配符 批量删除key
Redis 中 DEL指令支持多个key作为参数进行删除 但不支持通配符,无法通过通配符批量删除key,不过我们可以借助 Linux 的管道和 xargs 指令来完成这个动作. 比如要删除所有以use ...
- ContextLoaderListener和Spring MVC中的DispatcherServlet学习 随手记
Servlet上下文关系 DispatcherServlet的上下文是通过配置servlet的contextConfigLocation来加载的,默认实现是XmlWebApplicationConte ...
- 用Word 写csdn blog
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- SuSE Linux上修改主机名
1) 临时修改主机名 临时修改使用hostname即可,格式为:hostname 新主机名.Hostname命令除可以临时修改主机名外,还可以用它来查看主机名,不带参数执行它,即为查看主机名. 2) ...
- log4net 入门使用
log4net 是dotnet平台下的一个日记记录组件. 一 NuGet中安装log4net包: 二 配置log4net.config文件 配置文件内容: <?xml version=&quo ...