Problem:

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]
]

Summary:

输出杨辉三角的前n行。

Solution:

方法类似于LeetCode 119 Pascal's Triangle II

 class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
vector<int> v;
for (int i = ; i < numRows; i++) {
v.resize(i + );
v[] = v[i] = ;
for (int j = i - ; j > ; j--) {
v[j] = v[j - ] + v[j];
}
res.push_back(v);
} return res;
}
};

class Solution {public:    vector<vector<int>> generate(int numRows) {        vector<vector<int>> res;        vector<int> v;        for (int i = 0; i < numRows; i++) {            v.resize(i + 1);            v[0] = v[i] = 1;            for (int j = i - 1; j > 0; j--) {                v[j] = v[j - 1] + v[j];            }            res.push_back(v);        }                return res;    }};

LeetCode 118 Pascal's Triangle的更多相关文章

  1. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  2. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  3. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

  4. LeetCode 118. Pascal's Triangle (杨辉三角)

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

  5. leetcode 118 Pascal's Triangle ----- java

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

  6. Java [Leetcode 118]Pascal's Triangle

    题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  7. Java for LeetCode 118 Pascal's Triangle

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

  8. Leetcode 118 Pascal's Triangle 数论递推

    杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...

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

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

随机推荐

  1. 析构函数virtual与非virtual区别 [转]

    作为通常的原则,如果一个类定义了虚函数,那么它的析构函数就应当是virtual的.因为定义了虚函数则隐含着:这个类会被继承,并且会通过基类的指针指向子类对象,从而得到多态性.   这个类可能会被继承, ...

  2. 软件工程(FZU2015)赛季得分榜,第六回合

    目录 第一回合 第二回合 第三回合 第四回合 第五回合 第6回合 第7回合 第8回合 第9回合 第10回合 第11回合 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分: 团队项目分 ...

  3. maven 打包

    使用命令行形式打包 1.配置maven环境变量,在变量path中加入maven路径. 2.在要打包的项目目录下使用:Ctrl+shift+鼠标右键点击,点击 在此处打开命令行窗口. 在打开的命令行窗口 ...

  4. [转]extjs render 用法介绍

    renderer可以格式化该列显示的数据格式或者按照你自定义的脚本显示最终数据样子,个人是这么理解,如果你不是可以看下本文 复制代码 代码如下: var cm = new Ext.grid.Colum ...

  5. Python删除指定时间的文件

    import os import time import sys from xml.dom import minidom, Node from xml.dom.minidom import parse ...

  6. 数据结构图文解析之:栈的简介及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  7. Beta阶段第三次Scrum Meeting

    情况简述 Beta阶段第三次Scrum Meeting 敏捷开发起始时间 2016/12/12 22:00 敏捷开发终止时间 2016/12/13 22:00 会议基本内容摘要 讨论决定了APP的名称 ...

  8. Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)

     传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...

  9. 理解数据库的PDO处理的理念

    做第一份工作的时候,脑海里没有数据安全性的概念,从来没有网站被黑客盯上的事情.网站用户量也不大,虽然工作繁忙,但是只要代码上了线,基本上没有出过问题.在这个期间曾经做过一些傻的事情,认为sql写的越复 ...

  10. sphinx 配置文件全解析

    sphinx的配置文件是在配置的时候最容易出错的了: 我们先要明白几个概念: source:数据源,数据是从什么地方来的. index:索引,当有数据源之后,从数据源处构建索引.索引实际上就是相当于一 ...