Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
My code:
class Solution:
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows==0:
return []
if numRows==1:
return [[1]]
result=[[1]]
for i in range(1,numRows):
past_line=result[-1]
tmp_result=[]
tmp_result.append(past_line[0])
for k in range(len(past_line)-1):
tmp_result.append(past_line[k]+past_line[k+1])
tmp_result.append(past_line[-1])
result.append(tmp_result)
return result