题目:
Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.
给定一数n,回传帕斯卡三角形的第n+1列(因为n是index值)
帕斯卡三角形的定义详见118.
这两题基本上可以说是十分相像
差在118.要回传前n列,而这题只需回传特定一列
也就是我们要记住的不再是整个三角形
只要记住我们要推算的新列的上一列就好
class Solution: def getRow(self, rowIndex: int) -> List[int]: if rowIndex==0: return [1] ans=[1,1] for i in range(1,rowIndex): temp=[1] for j in range(len(ans)-1): temp.append(ans[j]+ans[j+1]) temp.append(1) ans=temp return ans
大体上跟上题一样
不过这题我们只要推算出新的一列就把上一列捨弃储存新的这列
不用特别用一个阵列去存所有列
然后再用此列推算出下一列
如此循环直到做出第rowIndex+1列便可回传
最后执行时间22ms(faster than 99.72%)
那我们下题见