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. kafka剖析(转)

    Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...

  2. [題解](貪心/堆)luogu_P2107小Z的AK計劃

    清明講過一道類似的,難度略大的:P3545 [POI2012]HUR-Warehouse Store 兩道題類似,都是暫時先把前面的加進候選集合里,如果超出限制的話就拿現在這個和前面的交換, 相當於不 ...

  3. 牛客寒假6-C.项链

    链接:https://ac.nowcoder.com/acm/contest/332/C 题意: 小B想给她的新项链染色. 现在有m种颜色,对于第i种颜色,小B有a_i单位的颜料,每单位颜料可以染项链 ...

  4. Codeforces Round #533(Div. 2) C.Ayoub and Lost Array

    链接:https://codeforces.com/contest/1105/problem/C 题意: 给n,l,r. 一个n长的数组每个位置可以填区间l-r的值. 有多少种填法,使得数组每个位置相 ...

  5. 牛客练习赛42C(枚举权值)

    传送门 思路:既然无法枚举每个情况,那就枚举每个出现过的权值,加和.那么每个权值出现了多少次呢?用总数减去一次都选不中这个数的次数即可,类似概率的方法. #include <bits/stdc+ ...

  6. SpirngMVC-JSON

    Springmvc默认用MappingJacksonHttpMessageConverter对json数据进行转换,需要加入jackson的包,如下: 配置json转换器 在注解适配器中加入messa ...

  7. python入门之实例-用户登录、注册

    用户密码存储文件db(其中用户和密码之间用$符合隔开): admin$123456 root$sdfk9f24 chy$654321 代码如下: def login(username,password ...

  8. 532 K-diff Pairs in an Array 数组中差为K的数对

    详见:https://leetcode.com/problems/k-diff-pairs-in-an-array/description/ C++: class Solution { public: ...

  9. avalon使用体验

    最近在用avalon做项目,使用的感受是,它确实会比angualr学习成本更低,我不需要花很多时间去了解它的功能,没有指令.没有服务,花一个晚上看看API就差不多能着手用了.avalon的视图它提供了 ...

  10. javascript 转化一个数字数组为function数组(每个function都弹出相应的数字)

    javascript 转化一个数字数组为function数组(每个function都弹出相应的数字) var arrNum = [2,3,4,5,6,10,7]; var arrFun = []; f ...