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 批量重命名

    import osll=os.listdir(".")c=0for f in ll: c=c+1 os.rename(f,str(c)+".jpg")

  2. 好用的批量改名工具——文件批量改名工具V2.0 绿色版

    我找了一个绿色免安装的软件来实现批量改名要求 下载地址:http://www.orsoon.com/Soft/14049.html#xiazai 添加图片后,开始改名.通过输入a#就可以将这些图片进行 ...

  3. 《MySQL Workbench数据建模与开发》

    <MySQL Workbench数据建模与开发> 基本信息 原书名:MySQL Workbench:Data Modeling & Development 原出版社: McGraw ...

  4. List集合中的数据按照某一个属性进行分组

    有的时候,我们需要在java中对集合中的数据进行分组运算.例如:Bill对象有money(float)和type(String)属性,现有个集合List<Bill>,需要按照Bill的ty ...

  5. Spring data jpa Specification查询关于日期的范围搜索

    代码: 时间格式化类型: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat s ...

  6. C#零基础入门07:打老鼠之面向对象重构

    一:前言 有了上面两节的知识,尤其是第六节之后,现在我们回过头看我们的打老鼠游戏,我们是不是会发现:这个程序也太不面向对象了.我们所有的代码逻辑都分布在Code-Hide中(UI的后台代码,称之为Co ...

  7. error “Device supports x86, but APK only supports armeabi-v7a”

    checkout the build.gradle from module:app. It turns out that there's a such config: ndk { abiFilters ...

  8. TensorFlow的离线安装

    主要通过whl方式进行配置. 1.1          Whl文件下载地址: https://www.lfd.uci.edu/~gohlke/pythonlibs/ 注意:必须安装numpy-mkl, ...

  9. Unit Testing of Spring MVC Controllers: “Normal” Controllers

    Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  10. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...