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]]
"""
ret = []
for i in range(numRows):
ret.append([1])
for j in range(1,i):
ret[i].append(ret[i-1][j]+ret[i-1][j-1])
if i >0:
ret[i].append(1)
return ret
主要就是每行处理,将开始的1和最末端的1单独加上。时间复杂度为O(n^2),在结果上直接进行处理,不需要额外空间。
转载于:https://www.cnblogs.com/sherylwang/p/5448828.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)