118. Pascal's Triangle

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]
] 输出Pascal三角形的前n行;每次利用前面已经生成的行来生成下一行。 代码如下:
 class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> pascal;
vector<int> ss;
if(numRows == )
{
return pascal;
}
if(numRows == )
{
ss.push_back();
pascal.push_back(ss);
return pascal;
}
pascal.push_back({});
for(int i = ; i <numRows; i++)
{
for(int j = ; j <= i; j++)
{
if(j == || j == i)
{
ss.push_back();
continue;
}
int n = pascal[i-][j-] + pascal[i-][j];
ss.push_back(n);
}
pascal.push_back(ss);
ss.clear();
}
return pascal;
}
};
 

leetcode 118的更多相关文章

  1. LeetCode 118. 杨辉三角

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

  2. 2017-3-6 leetcode 118 169 189

    今天什么都没发生 ================================================= leetcode118 https://leetcode.com/problems ...

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

  4. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

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

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

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

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

  7. LN : leetcode 118 Pascal's Triangle

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

  8. LeetCode 118:杨辉三角 II Pascal's Triangle II

    公众号:爱写bug(ID:icodebugs) 作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. Given a non-negative index k whe ...

  9. Java实现 LeetCode 118 杨辉三角

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

随机推荐

  1. 安装ipython import path error

    sudo pip install ipython --upgrade # 升级 版本兼容性问题,升级到最新版本的ipython 后,会报错 from pickleshare import Pickle ...

  2. HtmlparseUtil.java

    该类并不是一个通用的工具类,需要按自己的要求实现,这里只记录了Htmlparse.jar包的一些用法.仅此而已! 详细看这里:http://gundumw100.iteye.com/blog/7043 ...

  3. Replace JSON.NET with ServiceStack.Text in ASP.NET Web API

    Because ServiceStack.Text performs much better I recently stumbled across a comparison of JSON seria ...

  4. type和role属性有什么区别呢

    type是规定标签的类型,比如<input />标签中使用type="button"就是代表一个按钮 使用type="text" 就是一个文本框,t ...

  5. crm 4 IFRAME 元素隐藏

    function hidebtn(elementTitle) { var x = document.getElementsByTagName("LI"); for (i = 0; ...

  6. js 节流函数 throttle

    /* * 频率控制 返回函数连续调用时,fn 执行频率限定为每多少时间执行一次 * @param fn {function} 需要调用的函数 * @param delay {number} 延迟时间, ...

  7. Java SE 第二十四讲----static与final使用陷阱关键字

    1.对于final类型成员变量,一般来说有两种赋值方式: a)在声明final类型的成员变量时就附上初值 package com.cl.staticandfinal; public class Fin ...

  8. Linux下网络编程学习杂记

    1.TCP/IP协议的体系结构包含四层:应用层(负责应用程序的网络服务,通过端口号识别各个不同的进程)->传输层(传输控制层协议TCP.用户数据报协议UDP.互联网控制消息协议ICMP)-> ...

  9. (转) C# textbox 限制输入问题

    原理:e.handled代表这次按键动作是否交由用户自己处理,如果为true代表由用户处理,系统不再过问,这里的应用是拦截,即通知系统我要处理这个数据,但又不处理(丢弃数据),从而实现拦截的效果. 在 ...

  10. DatabaseError: no such table: django_session

    最近我也遇到这个问题了,从网上查了下,说是数据库同步出了问题,只需要运行如下命令:python manage.py syncdb就可以了 (这是django1.4之前的命令,1.4之后的是 pytho ...