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 ...
随机推荐
- file_get_contents无法请求https连接的解决方法 php开启curl
file_get_contents无法请求https连接的解决方法 方法1: PHP.ini默认配置下,用file_get_contents读取https的链接,就会如下错误: Warning: fo ...
- 274. H-Index论文引用量
[抄题]: Given an array of citations (each citation is a non-negative integer) of a researcher, write a ...
- SqlServer 分区视图实现水平分表
我们都知道在数据库数据量较多的时候,可数据进行水平扩展,如分库,分区,分表(也叫分区)等.对于分表的一个方案,就是使用分区视图实现. 分区视图允许将大型表中的数据拆分成较小的成员表.根据其中一列中的数 ...
- g2o 图优化
http://www.cnblogs.com/gaoxiang12/p/5244828.html 扩展里面csparse
- Spring 注解 整理
首先 在xml中配置 xmlns:context="http://www.springframework.org/schema/context" http://www.spring ...
- StackExchange.Redis实现Redis发布订阅
由于ServiceStack.Redis最新版已经收费,所以现在大家陆陆续续都换到StackExchange.Redis上了,关于StackExchange.Redis详细可以参看Github htt ...
- sql返回前N行
场景:返回每个客户最近的3个订单. 假设我们已经有一个POC索引(详情见http://www.cnblogs.com/xiaopotian/p/6821502.html),有两种策略来完成该任务:一种 ...
- css常用技巧:input提示文字;placeholder字体修改
1 很多网站都需要更改 <input>内部的placeholder 文字颜色属性:下面来介绍下这个技巧. 2 源代码: <!DOCTYPE html><html> ...
- ArcGIS 桌面远程连接带有端口号的SDE
首先配置远程连接 PostgreSQL数据库远程连接功能的开启 需要修改连个配置文件,默认位于 安装目录的data子文件夹下. 1.postgresql.conf 修改成监听所有ip地址的连接 ...
- [示例] Drag And Drop for FireMonkey (Win & macOS)
源码下载: https://github.com/OneChen/DragAndDrop 效果: