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. 关于MySQL集群架构优劣势与适用场景的分析与思考

    http://blog.itpub.net/25723371/viewspace-1977389/

  2. day01 包 权限修饰符 static final

  3. laravel-admin 自定义导出表单

    官方导出文档 laravel-admin自带的导出excel会导出与此模型关联的其他数据.所以参考官方文档调整代码 文章表:id,title,user_id 用户表:id,username //文章模 ...

  4. Codeforces Round #497 (Div. 2) A. Romaji

    Bryce1010模板 http://codeforces.com/contest/1008/problems #include <bits/stdc++.h> using namespa ...

  5. Ubuntu下修改权限时出现"unable to execute /bin/chmod: Argument list too long"

    Ubuntu14.04下修改权限时出现"unable to execute /bin/chmod: Argument list too long"   ,大概意思是卧槽,你这参数也 ...

  6. js 跨浏览器实现事件

    我们知道不同的浏览器实现事件是不同的,就比如说我们常见的有三种方法: 1,dom0处理事件的方法,以前的js处理事件都是这样写的. (function () { var p=document.getE ...

  7. public private protected 三种访问修饰符在c#中的区别

    1. public 公有的可见性:在类自身内部可见: 可被子类继承: 类外部可见 2. protected 受保护的可见性:在类自身内部可见: 可被子类继承: 类外部不可见 3. private 私有 ...

  8. git与GitHub(二)

    昨天在安装完git之后,出了一个问题,虽然暂时有解决的办法,但是由于电脑中了病毒并且配置低下等原因,这个问题的解决办法目前还有待考证.遇到的问题是这样的: 前提是想试一下git在命令行里的命令:于是: ...

  9. 【读书笔记】构建之法(CH7~CH8)

    MSF九大原则: 1. 推动信息共享与沟通:“谐”,Alert 2. 为共同的远景而工作:目标明确—用户/老板 3. 充分授权和信任: 4. 各司其职,对项目共同负责: 5. 交付增量的价值: 6. ...

  10. 阻止除root外的其他用户登录

    在对系统进行某些更新时,你可能不希望用户登录,这时可以使用/ e t c / n o l o g i n文件,大多数系统都提供这个文件.一旦在/ e t c目录中使用t o u c h命令创建了一个名 ...