LeetCode--118--杨辉三件I
问题描述:
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
方法1:temp_list存储当前层的列表,当层数大于1时,用temp2_list存储其上一层的值,根据规则进行相加求和,每行第一个和最后一个append(1).
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows == 0:
return []
if numRows == 1:
return [[1]]
z_list = []
temp_list = [1]
z_list.append(temp_list)
for i in range(2,numRows+1):
temp_list = []
for j in range(0,i):
if j < 1:
temp_list.append(1)
elif j >=1 and j < i - 1:
temp2_list = z_list[i - 2]
temp_list.append(temp2_list[j-1] + temp2_list[j])
elif j == i - 1:
temp_list.append(1)
z_list.append(temp_list)
return z_list
方法2:用s[-1]表示上一层的列表。
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if(numRows==0):
return([])
if(numRows==1):
return([[1]])
s=[[1]]
for i in range(1,numRows):
t=[]
for j in range(len(s[-1])+1):
if(j==0):
t.append(s[-1][0])
elif(j==len(s[-1])):
t.append(s[-1][-1])
else:
t.append(s[-1][j]+s[-1][j-1])
s.append(t)
return(s)
2018-09-10 21:01:58
LeetCode--118--杨辉三件I的更多相关文章
- LeetCode 118. 杨辉三角
118. 杨辉三角 给定一个非负整数numRows,生成杨辉三角的前numRows行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例 输入: 5 输出: [ [1], [1,1], [1,2 ...
- Java实现 LeetCode 118 杨辉三角
118. 杨辉三角 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], ...
- leetcode 118. 杨辉三角(python)
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5输出:[ [1], [1,1], [1,2,1], [1, ...
- LeetCode:杨辉三角【118】
LeetCode:杨辉三角[118] 题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: ...
- LeetCode(119. 杨辉三角 II)
问题描述 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: 你可以优化你的 ...
- C语言118. 杨辉三角
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5输出:[ [1], [1,1], [1,2,1], [1, ...
- Java实现 LeetCode 119 杨辉三角 II
119. 杨辉三角 II 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...
- Leecode刷题之旅-C语言/python-118杨辉三角
/* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-t ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
随机推荐
- ubuntu常用指令
总结一下常用的linux指令. mark一个linux指令学习和速查的网站:http://man.linuxde.net/ (0) su和sudo:得到root权限 su 切换到root用户 sudo ...
- presto 0.166概述
presto是什么 是Facebook开源的,完全基于内存的并⾏计算,分布式SQL交互式查询引擎 是一种Massively parallel processing (MPP)架构,多个节点管道式执⾏ ...
- Linux服务器配置---配置telnet
配置telnet 通过配置文件,我们可以设置telnet的连接时间.连接数.连接ip等,实现更加安全的连接 1.设置连接时间,参数“access_times” [root@localhost ...
- nohup 命令(设置后台进程): appending output to ‘nohup.out’ 问题
一.Linux 下使用 nohup Unix/Linux下一般比如想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行. 比如我们要运行weblogic在后台:./startW ...
- 远程登录 dos命令
1.桌面连接命令 mstsc /v: 192.168.1.250 /console 2.若需要远程启动所有Internet服务,可以使用iisreset命令来实现. 进入“命令提示符”窗口.在提示符后 ...
- 计算概论(A)/基础编程练习1(8题)/4:求一元二次方程的根
#include<stdio.h> #include<math.h> int main() { // 待解方程数目 int n; scanf("%d", & ...
- pyDay3
内容来自廖雪峰的官方网站 1.关键字参数 def person(**kw): print(kw) >>> person(name=') {'} 关键字参数有什么用?它可以扩展函数的功 ...
- swagger报错No handler found for GET /swagger-ui.html
今天下载jeeweb框架下来研究,其他还有,就是swagger老是出不来.报错:No handler found for GET /swagger-ui.html 后来搜索才发现,这个错误,是因为资源 ...
- WebSocket协议详解
转自 http://www.cnblogs.com/lizhenghn/p/5155933.html 1. websocket 是什么 websocket 是html5提出的一个协议规范,参考rfc6 ...
- 06: Django Admin
目录:Django其他篇 01:Django基础篇 02:Django进阶篇 03:Django数据库操作--->Model 04: Form 验证用户数据 & 生成html 05:Mo ...