-
Notifications
You must be signed in to change notification settings - Fork 0
/
#118 generate.py
32 lines (29 loc) · 954 Bytes
/
#118 generate.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
class Solution:
# More optimized solution
def generate(self, numRows: int) -> List[List[int]]:
ans = [[1] * (i + 1) for i in range(numRows)]
for i in range(2, numRows):
for j in range(1, i):
ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j]
return ans
# Original solution
# def generate(self, numRows: int) -> List[List[int]]:
# if numRows == 1:
# return [[1]]
#
# if numRows == 2:
# return [[1], [1, 1]]
#
# res = [[1], [1, 1]]
#
# for i in range(3, numRows + 1):
# res.append([1] * i)
#
# # Start from row 3 and onwards
# for row in range(2, len(res)):
#
# # Start from the second element to the 2nd to the last element
# for i in range(1, len(res[row]) - 1):
# res[row][i] = res[row - 1][i - 1] + res[row - 1][i]
#
# return res