Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.

Example:

Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

原题地址: Pascal's Triangle

难度: Easy

题意: 杨辉三角

class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
res = []
for i in range(numRows):
if i == 0:
row = [1]
else:
row = [1]
for j in range(1, i):
row.append(res[-1][j] + res[-1][j-1])
row.append(1)
res.append(row)
return res

时间复杂度: O(n)

空间复杂度: O(n)

118. Pascal's Triangle@python的更多相关文章

  1. 118. Pascal's Triangle

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

  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 、119. Pascal's Triangle II 、120. Triangle

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

  5. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

  6. 【LeetCode】118. Pascal's Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  7. [LeetCode&Python] Problem 118. Pascal's Triangle

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

  8. LeetCode 118 Pascal's Triangle

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

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

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

随机推荐

  1. C#主从表查询

    软件的使用必然涉及到主表和子表的操作,我们先在SQLite中创建子表.比如 创建一学生信息表做主表,再创建一个学生成绩表做子表.然后我们在程序中成绩 方法来连接子表. 判断bindingsource中 ...

  2. java 8 lamda Stream的Collectors.toMap 参数

    使用toMap()函数之后,返回的就是一个Map了,自然会需要key和value.toMap()的第一个参数就是用来生成key值的,第二个参数就是用来生成value值的.第三个参数用在key值冲突的情 ...

  3. win10怎么修改DNS

    方法/步骤   1 鼠标右键桌面单击此电脑--属性,如下图所示 2 进入电脑属性,选择控制面板主页,如下图所示 3 我们继续选择网络和Internet进入,如下图所示 4 进入网络和Internet, ...

  4. spring+mybits 整合所需jar包的下载路径(亲测有效)

    1.spring jar包:http://repo.springsource.org/libs-release-local/org/springframework/spring/5.0.0.RELEA ...

  5. 缺少mscvr100.dll

    最后使用百度电脑专家修复好的!

  6. SQL注入原理与解决方法代码示例

    一.什么是sql注入? 1.什么是sql注入呢? 所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如先前的很多影视网 ...

  7. How to detect the presence of the Visual C++ 2010 redistributable package

    Question: I have seen your previous blog posts that describe how to detect the presence of the Visua ...

  8. vue从入门到开发--1-安装脚手架

    一: 1.在文件目录下打开命令窗口(按住shift+右键[在此处打开命令窗口]或者直接ctrl+R打开命令窗口,利用cd选择到自己的文件目录) 2.$ npm install --global vue ...

  9. sublime前端插件以及常用快捷键

    29个常用 Sublime Text 插件推荐 来源:互联网 作者:佚名 时间:06-18 09:27:55 [大 中 小] Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,P ...

  10. CentOS7.2+MySQL5.7_ yum源方式_ 安装配置教程

    1)访问mysql官方网站 #访问网站 https://dev.mysql.com/downloads/file/?id=470281 2)下载安装包到linux #进入文件存放路径 cd /usr/ ...