Given an index k, return the kth row of the Pascal's triangle.spa
For example, given k = 3,
Return [1,3,3,1].code
Note:
Could you optimize your algorithm to use only O(k) extra space?get
這裏的關鍵是空間的使用,既然只能用O(K)很容易就想到咱們要進行回捲(名字好像不對)。個人作法是每一次都在後面新加入一個數it
class Solution: # @return a list of integers def getRow(self, rowIndex): ans = [1] * (rowIndex+1) for i in range(rowIndex): for j in range(i,0,-1): ans[j] += ans[j-1] #print ans return ans