0
点赞
收藏
分享

微信扫一扫

118.pascals-triangle


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]
]
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
ans = [[1] * n for n in xrange(1, numRows + 1)]
for i in range(1, numRows + 1):
for j in range(0, i - 2):
ans[i - 1][1 + j] = ans[i - 2][j] + ans[i - 2][j + 1]
return ans


举报

相关推荐

0 条评论