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

思路: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. Excel添加水印

    Excel添加水印[源码下载] 步骤一:根据生成图片的类创建水印图片 步骤二: 代码在Excel中根据第一行获取sheet的列数[sheet.getRow(0).getLastCellNum() ], ...

  2. shader之顶点着色器

    Vertex Shader 是渲染管道中一个可编程的模块,用于处理独立的顶点.Vertex Shader接收Vertex Attribute Data,由定点数组对象通过渲染指令来生成. Vertex ...

  3. Django 的 之 视图

    Django的View(视图) 一个视图函数(类),简称视图, 是个简单的python函数(类),它接受wed请求并且返回web 响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...

  4. linux下 卸载vmtools

    注意事项: 1) 安装linux时,一定要安装gcc和kernel-source: 2)光驱使用完毕可使用 umount /mnt/cdrom 卸载掉 3)VMtools 默认安装在 /usr/bin ...

  5. JQ 获取ul\ol 下面li的个数

    使用 jQuery 获取 ul 下面 li 的个数,那么我们需要遍历我们的ul.如果你的ul有class .id 或两者都没,您可以使用 ul 标签来遍历. //遍历ul 获取li个数 $(" ...

  6. 聊聊心跳机制及netty心跳实现

    我们在使用netty的时候会使用一个参数,ChannelOption.SO_KEEPALIVE为true, 设置好了之后再Linux系统才会对keepalive生效,但是linux里边需要配置几个参数 ...

  7. SSH 代码笔记

    os.path :https://www.cnblogs.com/wuxie1989/p/5623435.html .format():https://blog.csdn.net/i_chaoren/ ...

  8. 51nod1412(dp)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1412 代码: #include <bits/stdc+ ...

  9. java利用URL发送get和post请求

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  10. python 之 序列化与反序列化、os模块

    6.6 序列化与反序列化 特殊的字符串 , 只有:int / str / list / dict 最外层必须是列表或字典,如果包含字符串,必须是双引号"". 序列化:将Python ...