【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 ...
随机推荐
- Windows Server 2008 R2 64位操作系统安装Oracle 11g 64位数据库,在客户终端上安装Oracle 11g 32位,才能安装P/L Sql Developer并配置
1.下载Oracle 11g R2 for Windows的版本 下载地址: http://www.oracle.com/technetwork/database/enterprise-edition ...
- 3D Slicer 4.7.0 VS 2010 Compile 编译
花了将近一周的时间的,终于在VS2010成功的编译了最新版的3D Slicer 4.7.0,感觉快要崩溃了.Slicer用了20多个外部的库,全都要一起编译,完整编译一次起码要七八个小时,光VS的Ou ...
- MFC 应用程序中使用管道代码示意
STARTUPINFO sinf = {0}; PROCESS_INFORMATION pinf = {0}; SECURITY_ATTRIBUTES sa = {0}; HANDLE hPipeOR ...
- SSH框架下的表单重复提交
前几天做了一个功能,是在某个操作后,刷新父页面的,刷新时弹出了下面图的框: 网上查了之后发现这个框是表单重复提交时出现的.分析后发现,这个页面的上一个动作是form submit(在ssh框架下),这 ...
- Direct Visual-Inertial Odometry with Stereo Cameras
这对于直接方法是特别有益的:众所周知直接图像对准是非凸的,并且只有在足够准确的初始估计可用时才能预期收敛.虽然在实践中像粗到精跟踪这样的技术会增加收敛半径,但是紧密的惯性积分可以更有效地解决这个问题, ...
- TensorRT下安装pycuda
为了模型小型化,效率更高,使用TensorRT进行优化.前提是你必须要安装pycuda,可是费了我一番功夫.做一个笔记如下: 1.参考网址: https://wiki.tiker.net/PyCuda ...
- 启用hive hwi方法
hive启动hwi: ./hive --service hwi ls: cannot access /opt/cdh-5.3.6/hive-0.13.1/lib/hive-hwi-*.war: No ...
- Java与面向对象之随感(1)
大一下学期上完了c++课程,当时自我感觉很良好,认为对面向对象编程已经是身经百战了,但是上了院里HuangYu老师的Java课之后,才发现自己对于面向对象的编程风格的理解只在皮毛,着实惭愧不已. 假设 ...
- python3实现字符串的全排列的方法(无重复字符)
https://www.jb51.net/article/143357.htm 抛出问题 求任意一个字符串的全排列组合,例如a='123',输出 123,132,213,231,312,321.(暂时 ...
- 以CENTOS6.8系统为例部署ORACLE11g RAC和DNS配置
本文所需要的全部文档均位于文末附录中 本文章以两个节点为例进行安装 在virtual box创建虚拟机时,网卡1为桥接网卡,网卡二为仅主机模式 创建两块磁盘,模式设置为可共享,都添加到两个虚拟机中 在 ...