leetcode:Pascal's Triangle
一、 题目
经典题目,杨辉三角,输入行数。生成杨辉三角的数组。
二、 分析
首先,我们知道有例如以下规律:
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's Triangle的更多相关文章
- 【Leetcode】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 ...
- leetcode笔记:Pascal's Triangle
一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- LeetCode——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 ...
- 【leetcode】118. Pascal's Triangle
@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...
- leetcode - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- [LeetCode]Pascal's Triangle II
题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...
- LeetCode118:Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Pascal's Triangle I,II
题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...
随机推荐
- 【CODEFORCES】 A. Keyboard
A. Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- Android-PullToRefresh(一)
先讲下这篇写啥东西,也就是这家伙(chrisbanes)写的一个上拉下拉刷新的Demo,连接https://github.com/fengcunhan/Android-PullToRefresh 东西 ...
- Eclipse使用教程之精华篇
插件安装方法 插件大概有三种安装方法: 第一种:知道在线安装地址.Eclipse→Help→Install New Software...→地址栏(Work with)中输入安装地址→勾选要安装的插件 ...
- python如何获取公众号下面粉丝的openid
如何获取公众号下面粉丝的openid呢,首先要获取一个access_token,这个token可不是令牌(Token),如何获取这个access_token呢?有两种方法,方法如下: # -*- co ...
- 转MQTT压力测试之Tsung的使用
转自:http://www.cnblogs.com/lingyejun/p/7941271.html nTsung测试工具的基本测试命令为 Tsung -f ~/.tsung/mqtt.xml -l ...
- 二级指针 (C语言)
二级指针又叫双指针.C语言中不存在引用,所以当你试图改变一个指针的值的时候必须使用二级指针.C++中可以使用引用类型来实现. 下面讲解C中的二级指针的使用方法. 例如我们使用指针来交换两个整型变量的值 ...
- CYQ学习主要摘要2
数据库配置假设如下: <connectionStrings> <add name="Conn" connectionString="Se ...
- Decoration5:引入Actuator进行站点监控
1.添加依赖 2.重启应用 3.下图显示了一些默认的监控端点 这是数据可以在前台用来做饼图和柱状图什么的,不过实际上我们现在还用不到,于是就不深入研究
- line: 1: Syntax error: word unexpected (expecting ")")
开发板上运行可执行程序报出错误: line1: 1: Syntax error: word unexpected (expecting ")") 解决思路: 1.编译器的问题 用a ...
- Bootstrap学习笔记(9)--模态框(登录/注册弹框)
说明: 1. 上来一个ul先把登录和注册两个链接扔进去,ul的类nav,navbar-nav是导航条,navbar-right让他固定在右侧.每个li的里面,data-toggle="mod ...