-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path119. 杨辉三角 II
More file actions
24 lines (24 loc) · 1017 Bytes
/
119. 杨辉三角 II
File metadata and controls
24 lines (24 loc) · 1017 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
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
result = [[1]]*(rowIndex+1)
if rowIndex>=1:
result[1]=result[1]*2
for i in range(2,rowIndex+1):
result[i]=result[i]*(i+1)
for j in range(1,len(result[i])-1):
result[i][j]=result[i-1][j]+result[i-1][j-1]
return result[rowIndex]
# 和上一题思路一样
# 下面贴一个更快的
class Solution:
def getRow(self,rowIndex):
# j行的数据, 应该由j - 1行的数据计算出来.
# 假设j - 1行为[1,3,3,1], 那么我们前面插入一个0(j行的数据会比j-1行多一个),
# 然后执行相加[0+1,1+3,3+3,3+1,1] = [1,4,6,4,1], 最后一个1保留即可.
r = [1]
for i in range(1, rowIndex + 1):
r.insert(0, 0)
# 因为i行的数据长度为i+1, 所以j+1不会越界, 并且最后一个1不会被修改.
for j in range(i):
r[j] = r[j] + r[j + 1]
return r