forked from ChienJuiLin/leetcode_heprecsler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_6.py
More file actions
48 lines (41 loc) · 1.16 KB
/
leetCode_6.py
File metadata and controls
48 lines (41 loc) · 1.16 KB
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
44
45
46
47
48
class Solution:
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows == 0:
return []
elif numRows == 1:
return [[1]]
elif numRows == 2:
return [[1],[1,1]]
ans = [[1],[1,1]]
for i in range(3,numRows+1):
new_row = [1]
for x in range(i-2):
new_row.append(ans[i-2][x]+ans[i-2][x+1])
new_row.append(1)
ans.extend([new_row])
return ans
# 這方法在操作陣列的手段很騷又迂迴
'''
class Solution:
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
res = []
if numRows < 3:
for i in range(1,numRows+1):
res.append([1] * i)
else:
lastRow = self.generate(numRows - 1)
lastRowPt = lastRow[-1]
currRow = [1] + [0] * (numRows - 2) + [1]
for i in range(len(lastRowPt)-1):
currRow[i+1] = lastRowPt[i] + lastRowPt[i+1]
res = lastRow + [currRow]
return res
'''