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 ...
随机推荐
- 541. Reverse String II 指定翻转前k个的字符串
[抄题]: Given a string and an integer k, you need to reverse the first k characters for every 2k chara ...
- Excel数据透视表
Excel中每列是一个字段,每行是一条记录. 值字段设置,双击更改统计方法. 双击透视表中的数据可以看具体是哪些记录贡献的这些数据. 显示报表筛选页,生成多个工作簿.
- PBOC中文件结构,文件类型解析
1.明确两个规范,a. ISO7816 b.EMV规范/PBOC规范,二者的区别,7816是ISO制定的,是国际规范,而EMV规范是卡组织制定的,是遵循ISO7816规范的,PBOC是抄袭EMV规 ...
- 【转】jvm 堆内存 栈内存 大小设置
原文地址:http://blog.csdn.net/qh_java/article/details/46608395 4种方式配置不同作用域的jvm的堆栈内存! 1.Eclise 中设置jvm内存: ...
- $.each()与$(selector).each()区别
jQuery.each( collection, callback(indexInArray, valueOfElement) )可用于迭代任何集合,无论是“名/值”对象(JavaScript对象)或 ...
- TextView 垂直居中
需要区分的是这里的top,bottom,ascent,descent,baseline是指字内容的属性,通过getPaint().getFontMetricsInt()来获取得到.和字体内容的外部容 ...
- Leaflet入门:添加点线面并导入GeoJSON数据|Tutorial of Leaflet: Adding Points, Lines, Polygons and Import GeoJSON File
Web GIS系列: 1.搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3 2.使用GeoServer+QGIS发布WMTS服务 3.使 ...
- 正则表达式(javascript)
在开发过程中要要把一个css中的平移的x,y提取出来 ,正好把正则表达式学习了一下 'fsdfsdfsdf300pxfdsfd200pxfsdfsdf100px' 找出里面 px前面的数字: 经查资 ...
- NIOS II下基于中断的UART接收和发送设计示例代码
#include "sys/alt_stdio.h" #include "altera_avalon_uart_regs.h" #include "s ...
- vs2008编译opencv,不能copy CMakeVSMacros2.vsmacros
由于学习opencv,要查看源码文件,所以要先对opencv进行编译,可悲的是出错了 “不能copy CMakeVSMacros2.vsmacros” 通过上网查找资料,之所以出现这种情况,是因为 ...