118. Pascal's Triangle
https://leetcode.com/problems/pascals-triangle
Python
Recursive
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
if numRows == 1:
return [[1]]
if numRows == 2:
return [[1], [1, 1]]
return self._generate(3, numRows, [[1], [1, 1]])
def _generate(self, level, targetLevel, result):
if level > targetLevel:
return result
pre_level = result[-1]
current = [1]
for i in range(1, len(pre_level)):
pre = pre_level[i-1]
cur = pre_level[i]
current.append(pre+cur)
current.append(1)
result.append(current)
return self._generate(level+1, targetLevel, result)