Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
Note that the row index starts from 0.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 3 Output: [1,3,3,1]
Follow up:
Could you optimize your algorithm to use only O(k) extra space?
思路:跟上一个题类似,只保留当前行就可以,所以有个不断更新的过程
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
rownum = rowIndex+1
res = [1]
if rownum == 1:
return res
else:
for i in range(rownum):
prerow = res
res = [1]
for j in range(i-1):
res.append(prerow[j]+prerow[j+1])
res.append(1)
return res