-
Notifications
You must be signed in to change notification settings - Fork 20
/
PascalsTriangle.py
43 lines (39 loc) · 915 Bytes
/
PascalsTriangle.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
# Given numRows, generate the first numRows of Pascal's triangle.
#
# For example, given numRows = 5,
# Return
#
# [
# [1],
# [1,1],
# [1,2,1],
# [1,3,3,1],
# [1,4,6,4,1]
# ]
#
# Python, Python3 all accepted.
class PascalsTriangle:
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
results = []
if numRows == 0:
return results
if numRows == 1:
results.append([1])
return results
if numRows == 2:
results.append([1])
results.append([1, 1])
return results
tmp = self.generate(numRows - 1)
tmp_list = []
last = tmp[len(tmp) - 1]
tmp_list.append(1)
for i in range(1, len(last)):
tmp_list.append(last[i - 1] + last[i])
tmp_list.append(1)
tmp.append(tmp_list)
return tmp