Generate Pascals Triangle
~2 mins read
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
"""
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
"""
def generate_pascals_triangle(self, numRows: int) -> List[List[int]]:
pascal = list()
for i in range(1,numRows+1):
new_row = [1] * i
pascal.append(new_row)
for j in range(1,i-1):
pascal[i-1][j] = pascal[i-2][j-1] + pascal[i-2][j]
return pascal
assert generate_pascals_triangle(5) == [
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
]