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. python resize

    import sys import os sys.path.append('/usr/local/lib/python2.7/site-packages') sys.path.append('/usr ...

  2. uvc Android

    1) The kernel is V4L2 enabled, e.g.,CONFIG_VIDEO_DEV=yCONFIG_VIDEO_V4L2_COMMON=yCONFIG_VIDEO_MEDIA=y ...

  3. Svg.Js A标签,链接操作

    一.创建a标签,为a标签添加内容 <div id="svg1"></div> <script> //SVG.A 链接创建 var draw = ...

  4. Java Callable接口与Future接口的两种使用方式

    Java Callable.Future的两种使用方式Callable+Futurepublic class Test { public static void main(String[] args) ...

  5. JEECG 命名规范

    举例讲解代码规范 例如:表名 :jeecg_sys_demo 第一部分:代码文件命名规则如下: 首先:表名采用驼峰写法转换为Java代码使用单词  jeecg_sys_demo => Jeecg ...

  6. 查看JVM统计信息【转】

    查看JVM统计信息 [myname@name ~]$ jstat -gcutil 17421 Warning: Unresolved Symbol: sun.gc.generation.2.space ...

  7. AOP AspectJ 字节码 示例 Hugo MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. axios post提交的Content-Type

    使用axios的坑 jQuery.ajax的post提交默认的请求头的Content-Type: application/x-www-form-urlencoded而axios.post提交的请求头是 ...

  9. Zend Studio 实用快捷键大全

    编辑功能 组合键 实现功能 适用条件 Ctrl+/ 单行注释.当前为php代码时,则在光标所在行添加双斜杠行注释,选择多行则每一行都添加双斜杠:而当代码为html时则在行前后添加<!-- --& ...

  10. Java归去来第4集:java实战之Eclipse中创建Maven类型的SSM项目

    一.前言 如果还不了解剧情,请返回第3集的剧情          Java归去来第3集:Eclipse中给动态模块升级 二.在Eclipse中创建Maven类型的SSM项目 2.1:SSM简介 SSM ...