简繁EN

LeetCodeNo. 020

【LeetCode with Python】 118. Pascal's Triangle

目录

题目

原题页面:https://leetcode.com/problems/pascals-triangle/
题目类型:Array
难度评价:Easy
类似题目:(E) Pascal's Triangle II

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

分析

输出帕斯卡三角,明白帕斯卡三角的定义即可简单解题。


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
# @return a list of lists of integers
def generate(self, numRows):
rows = []
if numRows <= 0:
return rows
if numRows >= 1:
rows.append([1])
if numRows >= 2:
rows.append([1, 1])
if numRows >= 3:
need_rowsnum = numRows - 2
last_row = [1, 1]
for i in range(0, need_rowsnum):
new_row = [1]
for j in range(1, len(last_row)):
new_row.append(last_row[j - 1] + last_row[j])
new_row.append(1)
rows.append(new_row)
last_row = new_row
return rows

本文地址:https://xnerv.wang/leetcode-pascals-triangle/