-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path118.py
42 lines (33 loc) · 962 Bytes
/
118.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/pascals-triangle
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
"""
from typing import List
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
if numRows == 0:
return []
if numRows == 1:
return [[1]]
rets = [[1]]
for idx in range(1, numRows):
tmp = [1]
for jdx in range(idx-1):
tmp.append(rets[-1][jdx] + rets[-1][jdx+1])
tmp.append(1)
rets.append(tmp)
return rets
print(Solution().generate(5))