【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 ...
随机推荐
- Linux 下文件
Linux下文件的三个时间(Atime,Mtime,Ctime) [root@node ~]# stat install.log File: `install.log' Size: Blocks: ...
- hadoop 0.20.2伪分布式安装详解
adoop 0.20.2伪分布式安装详解 hadoop有三种运行模式: 伪分布式不需要安装虚拟机,在同一台机器上同时启动5个进程,模拟分布式. 完全分布式至少有3个节点,其中一个做master,运行名 ...
- 在Ubuntu环境下安装eclipse
Eclipse运行需要Java环境,java环境的安装见https://www.cnblogs.com/Sabre/p/10349320.html,本文不再赘述. 1.下载eclipse eclips ...
- Codeforces 1108D - Diverse Garland - [简单DP]
题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...
- UDP,TCP的套接字编程的Python实现
UDP,TCP的套接字编程的Python实现 套接字:连接应用层和运输层,应用层的网络应用程序使用IP地址+端口号来标识自己,然后通过套接字调用运输层为其服务,网络应用程序只能指定自己要使用的网络类型 ...
- React组件的State
React组件的State 1.正确定义State React把组件看成一个状态机.通过与用户的交互,实现不同状态,然后渲染UI,让用户界面和数据保持一致.组件的任何UI改变,都可以从State的变化 ...
- cocoa开发Mac小试笔记
急需纠正自己的错误认识,Mac开发和iOS开发UI显示.事件交互.生命周期等存在极大的差距 首先点击事件NSButton和UIButton完全是两个不同的东西 列表UITableView和NSTabl ...
- /etc/apt/sources.list" E212: Can't open file for writing解决方案
:w !sudo tee % > /dev/null 解决.
- OWA (Office Web Access)
exchange的web网页,可以enrich的打开,用起来还行outlook一样. 同事的chrome(under windows) 默认就是i这样的.也没装插件,也没有怎样. 我的chrome(u ...
- RHEL6.2的安装文档
1 Installing RHEL 6.2 1.1 开始安装 选择“Install or upgrade an existing system”: 1.2 光盘检测 选择“Skip”跳过安装介质的检查 ...