Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5

Output:

[

    [1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
] 解题思路:先根据输入的整数构建相应的行数(其中所有的值都设置为1), 然后在遍历求中间每一个位置的具体数。时间复杂度为O(n2), 空间复杂度为O(n2) (第一行一个, 第二行两个, 第三行三个, 以此类推累加公式 然后去除系数) 解决代码:
 class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
res = [[1]*(i+1) for i in range(numRows)] # 根据输入创建相应数据 for i in range(2, numRows): # 从第三行开始,前两行不需要计算。
for j in range(1,i):
res[i][j] = res[i-1][j-1] + res[i-1][j]
return res

   

【LeetCode每天一题】Pascal's Triangle(杨辉三角)的更多相关文章

  1. [LeetCode] Pascal's Triangle 杨辉三角

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

  2. [leetcode-118]Pascal's triangle 杨辉三角

    Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  3. LeetCode118. Pascal's Triangle 杨辉三角

    题目 给定行数,生成对应的杨辉三角 思考 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2 对于第偶数行,个数也是偶数,对于奇数行,个 ...

  4. Pascal's Triangle(杨辉三角)

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

  5. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  6. Leecode刷题之旅-C语言/python-118杨辉三角

    /* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-t ...

  7. 20190105-打印字母C,H,N,口等图像和杨辉三角

    1. 打印字母C ****** * * * * ****** def print_c(n): print('*' * n) for i in range(n): print('* ') print(' ...

  8. 1233: 输出杨辉三角前n行(Java)

    WUSTOJ 1233: 输出杨辉三角前n行 题目 原题链接 Description 输出杨辉三角前n行. Input 输入一个数n(n <= 9) Output 输出杨辉三角前n行.(注意行末 ...

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

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

随机推荐

  1. thinkPHP框架 简单的删除和修改数据的做法 和 模板继承的意思大概做法

    BiaodanController.class.php控制器页面 <?php namespace Admin\Controller; use think\Controller; class Bi ...

  2. POJ3628:Bookshelf 2【01背包】

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  3. API(一)之Serialization

    virtualenv is a tool to create isolated Python environments. 建立一个新的环境 Before we do anything else we' ...

  4. linux_check

    linux_check echo "********CPU****************" echo 总核数 = 物理CPU个数 X 每颗物理CPU的核数 echo " ...

  5. c++基本函数

    std::abs 求绝对值函数 double abs (double x); float abs (float x); long double abs (long double x); std::sw ...

  6. Robot Framework使用For循环

    1.普通的For循环 在一个普通的For循环中,循环开始的关键字是 :FOR ,其中的:用于与一般关键字做区分,对于循环结构体内的每一行,使用 \ 作为改行的行首关键字.对于循环中的变量,可以在 IN ...

  7. redis集群,主从,持久化

    1,单机版 先安装gcc   yum install gcc-c++ 然后解压源码包,执行编译命令make(C语言写的,需要gcc环境),最后安装Redis,需要通过PREFIX指定安装路径make ...

  8. 利用win10自带的虚拟机Hyper-V安装Centos7的步骤教程

    1.设置开启Hyper-V应用程序? 在搜索功能里输入 Hyper-V 然后点击选中的部分 2.全部选中框中的部分,然后重新启动电脑 3.在搜索功能里输入Hyper-V 打开 4.点击新建--> ...

  9. debian配置java环境变量

    操作如下 首先从Oracle上下载支持linux的java jdk,然后安装到你系统中, 然后到/home/xxxx/.bashrc文件中加入 # java JAVA_HOME=/xxx/xxxxx ...

  10. MVC 实用构架实战(一)——项目结构搭建

    一.前言 在<上篇>中,已经把项目整体结构规划做了个大概的规划.在本文中,将使用代码的方式来一一解说各个层次.由于要搭建一个基本完整的结构,可能文章会比较长.另外,本系列主要出于实用的目的 ...