Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
给定k,返回第k行的杨辉三角,最好使用O(k)的空间复杂度
代码如下:class Solution:
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
res = [1]
rowNum = rowIndex+1
if rowNum == 1:
return res
if rowNum > 0:
res = [1]
for index in range(rowNum):
las = res
res = [1]
for j in range(index-1):
res.append(las[j] + las[j+1])
res.append(1)
return res
Note:
Could you optimize your algorithm to use only O(k) extra space?
给定k,返回第k行的杨辉三角,最好使用O(k)的空间复杂度