给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

  1. 示例:
    输入: 5
  2. 输出:
  3. [
  4. [1],
  5. [1,1],
  6. [1,2,1],
  7. [1,3,3,1],
  8. [1,4,6,4,1]
  9. ]

  

  1. class Solution(object):
  2. def generate(self, numRows):
  3. """
  4. :type numRows: int
  5. :rtype: List[List[int]]
  6. """
  7. result = []
  8. for i in range(numRows):
  9. temp = [0] * (i + 1)
  10. temp[0],temp[-1] = 1,1
  11. for j in range(1, len(temp) - 1):
  12. temp[j] = result[i - 1][j - 1] + result[i - 1][j]
  13. result.append(temp)
  14. return result

LeetCode118 杨辉三角 Python的更多相关文章

  1. [Swift]LeetCode118. 杨辉三角 | Pascal's Triangle

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

  2. LeetCode118.杨辉三角

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

  3. 打印杨辉三角—Python

    b=[] for i in range(0,9): c=[] for j in range(0,i): if j==0: c.append(b[i-1][j]) if j<=i-2:#执行完第一 ...

  4. 杨辉三角-python

    # -*- coding: utf-8 -*- def triangles(): yield [1] # n = 0 第一行 yield [1, 1] # n = 1 第二行 b, n, old = ...

  5. 杨辉三角python的最佳实现方式,牛的不能再牛了

    def triangles(): N = [1] while True: yield N N.append(0) N = [N[i-1] + N[i] for i in range(len(N))] ...

  6. 杨辉三角的Python实现

      杨辉三角的Python实现 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Python生成器实现杨辉三角: # python def yanghui_tr ...

  7. python 生成器生成杨辉三角

    用Python写趣味程序感觉屌屌的,停不下来 #生成器生成展示杨辉三角 #原理是在一个2维数组里展示杨辉三角,空的地方用0,输出时,转化为' ' def yang(line): n,leng=0,2* ...

  8. python实现杨辉三角

    刚刚学python,原来用c++,Java很轻松实现的杨辉三角,现在用python实现,代码是少了,理解起来却不容易啊. 这里主要用到的Python的生成器. 我们都知道Python有列表解析功能,根 ...

  9. python 实现杨辉三角(依旧遗留问题)

    1 #! usr/bin/env python3 #-*- coding :utf-8 -*- print('杨辉三角的generator') def triangles(): N=[1] while ...

随机推荐

  1. pat乙级1049

    浮点型乘整型和整型乘浮点型结果不同,不知为什么. double sum = 0.0; ; i < n; i++) { cin >> a[i]; sum += a[i] * (i + ...

  2. docker 快速搭建 mysql

    准备工作 系统 centos7 切换阿里源 #备份资源文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo ...

  3. ZOJ-1654 Place the Robots---二分图最小点覆盖+构图

    题目链接: https://vjudge.net/problem/ZOJ-1654 题目大意: 有一个N*M(N,M<=50)的棋盘,棋盘的每一格是三种类型之一:空地.草地.墙.机器人只能放在空 ...

  4. iOS keychain注解

    + (NSMutableDictionary *)getKeychainQuery:(NSString *)service { return [NSMutableDictionary dictiona ...

  5. 【转】chrome浏览器的跨域设置——包括版本49前后两种设置

    做前后分离的webapp开发的时候,出于一些原因往往需要将浏览器设置成支持跨域的模式,好在chrome浏览器就是支持可跨域的设置,网上也有很多chrome跨域设置教程.但是新版本的chrome浏览器提 ...

  6. cd ..和cd -

    cd ..是返回上一层目录, cd -是返回到上一次的工作目录.

  7. 漫谈Ajax在.Net中的使用

    引用地址:http://birdshover.cnblogs.com/archive/2006/07/03/441439.html AJAX出来的时间也不短了.虽然它在某些方面很受争议,但是瑕不掩瑜. ...

  8. iOS 中push和pop到底系统做了些什么事

    iOS中的push和pop是一个很常用的视图切换方法,他们是成对出现的, 简而言之,push就是压栈,pop就是出栈! [self.navigationController pushViewContr ...

  9. ZendFramework-2.4 源代码 - ViewManager类图

  10. 微信小程序 onLoad 函数

    小程序注册完成后,加载页面,触发onLoad方法. 页面载入后触发onShow方法,显示页面. 首次显示页面,会触发onReady方法,渲染页面元素和样式,一个页面只会调用一次. 当小程序后台运行或跳 ...