一、     题目

经典题目,杨辉三角,输入行数。生成杨辉三角的数组。

二、     分析

首先,我们知道有例如以下规律:

1、每一行的第一个数和最后一个数都为1

2、中间的数是上面数和上面数左边的数的和值

须要注意的是,当行数为0时输出[[1]]

结果为一个二维数组,所以不难想到解决方式。

每层保存前一行的指针,然后当前行数据依据上一行来得到,每一个元素就是上一行两个相邻元素相加(第一个和最后一个元素是1)。

算法时间复杂度应该是O(1+2+3+...+n)=O(n^2),空间上仅仅须要二维数组来存储结果。不须要额外空间。:

<span style="font-size:18px;">//方法一:
class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int>> cls;
for(int i=0;i < numRows;i++) {
vector<int> flag;
if(i<=0) //flag.push_back(1);
return cls;
else {
for(int j=0;j<=i;j++) {
if(j==0||j==i) flag.push_back(1);
else
flag.push_back(cls[i-1][j-1]+cls[i-1][j]);
}
}
cls.push_back(flag);
}
return cls;
}
}; //方法二:
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> cls;
if(numRows<=0) return cls;
cls.push_back(vector<int>(1,1));
for(int i=1;i<numRows;i++) {
int cur_size = (int)cls[i-1].size();
vector<int> cur_ver;
cur_ver.push_back(1);
for(int j=1;j<cur_size;j++) {
int flag = cls[i-1][j]+cls[i-1][j-1];
cur_ver.push_back(flag);
}
cur_ver.push_back(1);
cls.push_back(cur_ver);
}
return cls;
}
}; </span>

leetcode:Pascal&#39;s Triangle的更多相关文章

  1. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  2. leetcode笔记:Pascal&#39;s Triangle

    一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...

  3. LeetCode——Pascal&#39;s Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  4. LeetCode——Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 【leetcode】118. Pascal&#39;s Triangle

    @requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...

  6. leetcode - Pascal&#39;s Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  7. [LeetCode]Pascal&#39;s Triangle II

    题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...

  8. LeetCode118:Pascal&#39;s Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  9. Pascal&#39;s Triangle I,II

    题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...

随机推荐

  1. QtGui.QFontDialog

    The QtGui.QFontDialog is a dialog widget for selecting a font. #!/usr/bin/python # -*- coding: utf-8 ...

  2. Maven--要点笔记

    --maven笔记 1.maven命令 2.archetype插件:用于创建符合maven规定的文件夹骨架   命令: mvn archetype:generate 3. 坐标:构件 仓库: 本地仓库 ...

  3. c#:无法将类型为“System.DBNull”的对象强制转换为类型“System.String”

    解决办法: 使用转换函数即可: Convert.ToString(要转换的值);

  4. 查看Linux磁盘空间大小命令

    发表于:2012-09-17 10:25   查看Linux磁盘空间大小 一.df 命令: df 是来自于coreutils 软件包,系统安装时,就自带的:我们通过这个命令可以查看磁盘的使用情况以及文 ...

  5. 最大似然估计(MLE)与最大后验概率(MAP)

    何为:最大似然估计(MLE): 最大似然估计提供了一种给定观察数据来评估模型参数的方法,即:“模型已定,参数未知”.可以通过采样,获取部分数据,然后通过最大似然估计来获取已知模型的参数. 最大似然估计 ...

  6. VMware Workstation unrecoverable error: (vmx)虚拟机挂起后无法启动问题

    为了方便,虚拟机都是采用挂起状态,今天在启动虚拟机的时候出现如下提示错误: VMware Workstation unrecoverable error: (vmx)Exception 0xc0000 ...

  7. C#中Out和Ref參数修饰符

    在编程过程中对于函数之间的參数的传递一般分为两种:传值和传地址. 以下为大家分析一下. 传值 比方你又一份文档,假设採用传值的话.相当于我复制了一份,因此我对我这份文档的改动都不会影响到你的那份.假设 ...

  8. C# BackgroundWorker的Bug???

    废话不多说,上代码: public partial class Form1 : Form { BackgroundWorker _bgWorker; int count; public Form1() ...

  9. 小程序 wx.request ajax示例

    简单示例 https://developers.weixin.qq.com/miniprogram/dev/api/network-request.html wx.request({ method: ...

  10. 何时应该使用Directive、Controller、Service?

    AngularJS:何时应该使用Directive.Controller.Service? 大漠穷秋 译 AngularJS是一款非常强大的前端MVC框架.同时,它也引入了相当多的概念,这些概念我们可 ...