# Leetcode 119. Pascal's Triangle II ## 題解 ### DP ```python! class Solution: def getRow(self, rowIndex: int) -> List[int]: output = [1] if rowIndex == 0: return output for y in range(1,rowIndex+1): next_output = [] for x in range(y+1): left = output[x-1] if x-1 >= 0 else 0 right = output[x] if x < len(output) else 0 next_output.append(left+right) output = next_output return output ``` #### 狀態壓縮 ```python! class Solution: def getRow(self, rowIndex: int) -> List[int]: i = 0 output = [] while i <= rowIndex: for j in range(len(output) - 1, 0 , -1): output[j] += output[j-1] output.append(1) i += 1 return output ```