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. ruby不能识别中文的一个坑

    需要安装readline,并重装ruby,挂readline编译. 后续发现总出错,然后发现brew包依赖不全,按照提示,一个一个的安装依赖.安装后使用rvm重装ruby就ok了. 多亏能够有办法上g ...

  2. Apache配置手札

    一.绑定域名到子目录 在httpd.conf文件末尾添加 #不同的域名对应到的目录 <VirtualHost *:80> DocumentRoot "D:\wamp\www\ba ...

  3. C#进阶系列——DDD领域驱动设计初探(七):Web层的搭建

    前言:好久没更新博客了,每天被该死的业务缠身,今天正好一个模块完成了,继续来完善我们的代码.之前的六篇完成了领域层.应用层.以及基础结构层的部分代码,这篇打算搭建下UI层的代码. DDD领域驱动设计初 ...

  4. Oracle系列——开发中奇葩问题你遇到几个(一)

    前言:在使用oracle数据进行开发的时候有没有经常出现一些很奇怪.很纳闷.很无厘头的问题呢.下面是本人使用oracle一段时间遇到的问题小节,在此做个记录,方便以后再遇到类似的问题能快速解决.如果你 ...

  5. 移动web开发—页面头部 META 总结

    meta指元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. 标签位于文档的头部,不包含任何内容. 标签的属性定义了与文档相关联的名称/值对. ...

  6. Log4j 简单应用

    #输出日志的包路径log4j.logger.com=DEBUG,FILE log4j.rootLogger=WARN,stdout #控制台日志 log4j.appender.stdout=org.a ...

  7. bzoj3343

    3343: 教主的魔法 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1178  Solved: 527[Submit][Status][Discus ...

  8. QQ个人文件夹中的文件被占用,解决办法

    我的情况是记住密码的账号不可以登录,不记住密码的账号确可以登录,突然就这样,我也很郁闷. 找到路径C:\Users\Public\Documents\Tencent\QQ下的UserDataInfo. ...

  9. php魔术方法使用场景

    php魔术方法-----__tostring(),__invoke,__call(),__callStatic ... __tostring(),__invoke() __tostring()方法是在 ...

  10. base64和图片的转换

    /// <summary> /// base64转图片 /// </summary> /// <param name="strBase64">& ...