Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,

Return

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

Subscribe to see which companies asked this question

//解题思路:利用一个中间vector来保存每层的数
class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int>> ans;
for(int i = 0;i < numRows;i++)
{
vector<int> cur;
if(i == 0)
cur.push_back(1);
else
{
for(int j = 0;j <= i;j++)
{
if(j == 0 || j == i) cur.push_back(1);
else cur.push_back(ans[i - 1][j] + ans[i - 1][j - 1]);
}
}
ans.push_back(cur);
} return ans;
}
};

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

  1. 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 = 5, Retu ...

  3. 【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 ...

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. HttpContext.Current.Session 和 Session 的区别

    Session(会话)通常指一个动作从开始到结束不间断的一个动作. 例如“打电话”,通常是“1.拿起电话--2.拨对方号码--3.对方截图--4.挂机”.这四个步骤从完成到结束组成了一个基本的Sess ...

  2. GAN与NLP的讨论

    https://www.jianshu.com/p/32e164883eab 这篇文章,GAN与NLP的讨论,可以看看.

  3. JS里关于事件的常被考察的知识点:事件流、事件广播、原生JS实现事件代理

    1.JS里面的事件流 DOM2级事件模型中规定了事件流的三个阶段:捕获阶段.目标阶段.冒泡阶段,低版本IE(IE8及以下版本)不支持捕获阶段 捕获事件流:Netscape提出的事件流,即事件由页面元素 ...

  4. 【算法】Java-Redis-Hash算法对比-参考资料

    Java-Redis-Hash算法对比-参考资料 redis java map 红黑树_百度搜索 java使用redis缓存(String,bean,list,map) - CSDN博客 redis ...

  5. 条件随机场(CRF)原理和实现

    版权声明:作者:金良山庄,欲联系请评论博客或私信,个人主页:http://www.jinliangxu.com/,CSDN博客: http://blog.csdn.net/u012176591   目 ...

  6. ios 内存管理总结

    在ios 中 项目有两个内存管理方式 第一种,arc 方式,编译器编译时,自动给obj 加上 release  实现要求 1. 设置项目 将 Objective-C Automatic Referen ...

  7. Spring(七):IOC&DI

    什么是IOC? IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源. 而应用了IOC之后, ...

  8. (转)真正的中国天气api接口xml,json(求加精) ...

      我只想说现在网上那几个api完全坑爹有木有??? 官方的申请不来有木有,还有收费有木有?? 咱这种菜鸟只能用免费的了!!!! http://m.weather.com.cn/data/101110 ...

  9. 定制加载loading 图片

    项目中要使用一个动态加载图片,找了好久都没有合适的最后发现了这个网站,自由定制需要的gif图片,完全免费啊 http://preloaders.net/en/

  10. Android NDK开发篇(六):Java与原生代码通信(异常处理)

    一.捕获异常 异常处理是Java中的功能.在Android中使用SDK进行开发的时候常常要用到.Android原生代码在运行过程中假设遇到错误,须要检測,并抛出异常给Java层.运行原生代码出现了问题 ...