【LeetCode每天一题】Pascal's Triangle(杨辉三角)
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(杨辉三角)的更多相关文章
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [leetcode-118]Pascal's triangle 杨辉三角
Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- LeetCode118. Pascal's Triangle 杨辉三角
题目 给定行数,生成对应的杨辉三角 思考 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2 对于第偶数行,个数也是偶数,对于奇数行,个 ...
- Pascal's Triangle(杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [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, ...
- Leecode刷题之旅-C语言/python-118杨辉三角
/* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-t ...
- 20190105-打印字母C,H,N,口等图像和杨辉三角
1. 打印字母C ****** * * * * ****** def print_c(n): print('*' * n) for i in range(n): print('* ') print(' ...
- 1233: 输出杨辉三角前n行(Java)
WUSTOJ 1233: 输出杨辉三角前n行 题目 原题链接 Description 输出杨辉三角前n行. Input 输入一个数n(n <= 9) Output 输出杨辉三角前n行.(注意行末 ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
随机推荐
- i.e., e.g., etc.
经常搞混的一些英语缩写,以及他们的应用规范. i.e. (id est) 含义为:也就是说,that is to say, in other words e.g. (exampli gratia) 含 ...
- genieacs Installation on Ubuntu14.04
Beside the installation guide on the main page, here is a guide to install GenieACS off a freshly in ...
- Pthon Matplotlib 画图
一.普通绘图 import matplotlib.pyplot as plt import numpy as np # 绘制普通图像 x = np.linspace(-1, 1, 50) y1 = 2 ...
- day_5.20 py
列表去重的两种方法 1.自己写程序 for循环 2.直接变为set类型
- Xshell登录Ubuntu12.04
Ubuntu安装ssh服务: sudo apt-get install openssh-server 打开Xshell,选择“新建”,“连接”设置里选择SSH,主机填入需要连接的主机的IP地址.在“用 ...
- Python把两个列表合成一个字典
简单粗暴上代码 A= [] B = [] C= dict(map(lambda x,y:[x,y],A,B)) 酱紫,就合成了一个字典
- jenkins编辑报错Exception when publishing, exception message的解决办法
jenkins编辑报错Exception when publishing, exception message的解决办法 查看目标主机的磁盘空间是否占满,清理磁盘空间即可
- authentication plugin caching_sha2
操作系统:windows 10 mysql版本:mysql Ver 8.0.11 for Win64 on x86_64 (MySQL Community Server - GPL) 安装完mysq ...
- [No0000E4]C# 常量
常量是固定值,程序执行期间不会改变.常量可以是任何基本数据类型,比如整数常量.浮点常量.字符常量或者字符串常量,还有枚举常量. 常量可以被当作常规的变量,只是它们的值在定义后不能被修改. 整数常量 整 ...
- linux:基本指令touch, cp 和 mv
touch 新建 #touch 的使用很简单, 我们先去往 Documents 的文件夹, 里面已经有了 folder1 和 file1, 如果我们想新建一个 file2 使用下面的语句就好. 一个空 ...