-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpascal2.py
More file actions
26 lines (21 loc) · 759 Bytes
/
pascal2.py
File metadata and controls
26 lines (21 loc) · 759 Bytes
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
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows == 0:
return []
pascal_triangle = [[1], [1,1]] # Constant values which are common for any pascal triangle
row = 2
while row < numRows:
previous = pascal_triangle[-1]
current = []
for i in range(len(previous)+1):
if i-1 < 0 or i+1 > len(previous):
current.append(1)
else:
current.append(previous[i]+previous[i-1])
pascal_triangle.append(current)
row += 1
return pascal_triangle[:numRows]