题意:给一个数字,返回一个二维数组,包含一个三角形。

思路:n=0、1、2都是特例,特别处理。3行以上的的头尾都是1,其他都是依靠上一行的两个数。具体了解Pascal三角形原理。

 class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int> > ans;
if(!numRows) return ans;
vector<int> tmp;
tmp.push_back();ans.push_back(tmp);if(numRows==) return ans;
tmp.push_back();ans.push_back(tmp);if(numRows==) return ans; for(int i=; i<numRows; i++)
{
tmp.clear();
tmp.push_back();
for(int j=; j<i; j++)
{
tmp.push_back( ans[i-][j-]+ans[i-][j] );
}
tmp.push_back();
ans.push_back(tmp);
}
return ans;
}
};

Pascal's Triangle

LeetCode Pascal's Triangle Pascal三角形的更多相关文章

  1. 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II

    118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  2. LeetCode Pascal's Triangle && Pascal's Triangle II Python

    Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...

  3. 28. Triangle && Pascal's Triangle && Pascal's Triangle II

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  4. Pascal's Triangle,Pascal's Triangle II

    一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...

  5. 118 Pascal's Triangle 帕斯卡三角形 杨辉三角形

    给定 numRows, 生成帕斯卡三角形的前 numRows 行.例如, 给定 numRows = 5,返回[     [1],    [1,1],   [1,2,1],  [1,3,3,1], [1 ...

  6. LeetCode OJ:Triangle(三角形)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. 【LeetCode】118. Pascal's Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  8. [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, ...

  9. [LeetCode] Pascal's Triangle 杨辉三角

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

随机推荐

  1. Auto Layout Guide----(三)-----Anatomy of a Constraint

    Anatomy of a Constraint 剖析约束 The layout of your view hierarchy is defined as a series of linear equa ...

  2. qt5.3+vs2013乱码

    解决qt5.3+vs2013乱码,在main函数之前加入 #if _MSC_VER >= 1600 #pragma execution_character_set("utf-8&quo ...

  3. CI框架中,扩展验证码类。

    使用CI框架的朋友,应该都知道CI框架的的验证码辅助函数,不太好用.它需要写入到数据库中,然后再进行比对. 大家在实际项目中,好像不会这样去使用,因为会对数据库造成一定的压力. 所以,我们还是利用se ...

  4. Android开发中,那些让你觉得相见恨晚的方法、类或接口

    Throwable类中的getStackTrace()方法,根据这个方法可以得到函数的逐层调用地址,其返回值为StackTraceElement[],而在StackTraceElement类中有四个方 ...

  5. LeetCode: 598 Range Addition II(easy)

    题目: Given an m * n matrix M initialized with all 0's and several update operations. Operations are r ...

  6. 火狐restclient安装和使用

    RESTClient是一款用于测试各种Web服务的插件,它可以向服务器发送各种HTTP请求(用户也可以自定义请求方式),并显示服务器响应.使用RESTClient您可以方便的测试各种Web服务,为您的 ...

  7. Java IO 输入和输出流

    数据流是指一组有顺序的,有起点和终点的字节集合. 最初的版本中,java.io 包中的流只有普通的字节流,即以 byte 为基本处理单位的流.字节流用来读写 8 位的数据,由于不会对数据做任何转换,因 ...

  8. 5.格式化输出f

    16.1 不区分大小写 num = input('>>>') s = F'python{num}' print(s) 16.2 可以加入表达式 s1='alex' s2=f'我的名字 ...

  9. How to generate a CSR in Microsoft IIS 7

    How to generate a CSR in Microsoft IIS 7 To help you generate your CSR for Microsoft IIS 7 we've pre ...

  10. 洛谷P2912 牧场散步Pasture Walking

    题目描述 The \(N\) cows (\(2 \leq N \leq 1,000\)) conveniently numbered \(1..N\) are grazing among the N ...