跳到主要内容

Pascal's Triangle II

描述

Given an index k, return the k-th row of the Pascal's triangle.

For example, given k = 3,

Return [1,3,3,1].

Note: Could you optimize your algorithm to use only O(k) extra space?

分析

滚动数组。

代码

# Pascal's Triangle II
# 滚动数组,时间复杂度O(n^2),空间复杂度O(n)
def get_row(row_index):
array = []
for i in range(row_index + 1):
for j in range(i - 1, 0, -1):
array[j] = array[j - 1] + array[j]
array.append(1)
return array

相关题目