题目:
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.
思路:用一个ans数组来存储答案,一开始ans=[1]。采用迭代的方法,每次利用已有的ans和一个临时数组temp生成下一行,再复制到ans里。
代码:
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
ans=[1]
for i in range(rowIndex):
temp = [1]
for j in range(len(ans)-1):
temp.append(ans[j]+ans[j+1])
temp.append(1)
ans = temp[:]
return ans