Note that row[j]=prev_row[j] + prev_row[j-1]
. Therefore, we can rewrite the values into the same array as long as we make a copy of prev_row[j]
, because we still need it when computing row[j+1]
.
class Solution:
# @param A : integer
# @return a list of integers
def getRow(self, A):
# kth row has k + 1 items
row = [0] * (A + 1)
row[0] = 1 # the 0th row
for i in range(1, A + 1):
t = 0 # [j - 1] of previous row
for j in range(i + 1):
temp = row[j]
row[j] = temp + t
t = temp
return row