# 119.pascals-triangle-ii

## Statement

• Difficulty: Easy
• Tag: `数组` `动态规划`

``````输入: rowIndex = 3

``````

``````输入: rowIndex = 0

``````

``````输入: rowIndex = 1

``````

• `0 <= rowIndex <= 33`

• Difficulty: Easy
• Tag: `Array` `Dynamic Programming`

Given an integer `rowIndex`, return the `rowIndexth` (0-indexed) row of the Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:

``````Input: rowIndex = 3
Output: [1,3,3,1]
``````

Example 2:

``````Input: rowIndex = 0
Output: [1]
``````

Example 3:

``````Input: rowIndex = 1
Output: [1,1]
``````

Constraints:

• `0 <= rowIndex <= 33`

Follow up: Could you optimize your algorithm to use only `O(rowIndex)` extra space?

## Solution

``````from typing import List

class Solution:
def getRow(self, rowIndex: int) -> List[int]:
res = [[1]]
pre = res[0]
for _ in range(rowIndex):
cur_res = [1]

for i in range(len(pre) - 1):
cur_res.append(pre[i] + pre[i + 1])

cur_res.append(1)
res.append(cur_res)
pre = cur_res
return res[-1]
``````