Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
Python Solution 1:
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows < 1:
return []
res = [[1]]
i = 1
while i < numRows:
i += 1
pre = res[-1]
new = [1]
for j in range(1, len(pre)):
new.append(pre[j] + pre[j - 1])
new.append(1)
res.append(new)
return res
Python Solution 2:
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows < 1: return []
res = [[1]]
for i in range(1, numRows):
res.append(map(lambda x, y: x + y, [0] + res[-1], res[-1] + [0]))
return res
Summary:
- nothing to say
LeetCode: 118. Pascal's Triangle





近期评论