-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0118-pascals-triangle_test.py
49 lines (40 loc) · 1.21 KB
/
0118-pascals-triangle_test.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
43
44
45
46
47
48
49
from typing import List
class Solution:
"""
Source: https://leetcode.com/problems/pascals-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]]
"""
@staticmethod
def generate(num_rows: int) -> List[List[int]]:
ans = []
for n in range(num_rows):
if n == 0:
ans.append([1])
elif n == 1:
ans.append([1, 1])
else:
previous_row = ans[-1]
e1 = 0
new_row = []
for e in previous_row:
e2 = e
new_row.append(e1 + e2)
e1 = e2
new_row.append(1)
ans.append(new_row)
return ans
def test_generate():
assert Solution.generate(5) == [[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1]]