-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascal'sTriangle.py
More file actions
37 lines (35 loc) · 835 Bytes
/
Copy pathPascal'sTriangle.py
File metadata and controls
37 lines (35 loc) · 835 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
27
28
29
30
31
32
33
34
35
36
37
"""
@Project: leetcode
@file: Pascal'sTriangle.py
@author: AC
@time: 2016/5/9 0:39
@Description: 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]
]
"""
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows == 0:
return []
else:
result = [[1]]
i, temp = 1, [0,1,0]
while i<numRows:
p1 = []
for j in xrange(len(temp)-1):
p1.append(temp[j]+temp[j+1])
result.append(p1)
i += 1
temp = [0]+p1+[0]
return result