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]
]
a[n,k]={a[n−1,k−1]+a[n−1,k]10<k<n−1k=0ork=n−1
class Solution(object):
def generate(self, numRows):
ans=[]
if numRows<1:
return ans
for i in range(numRows):
temp=[]
for k in range(i+1):
if k==0 or k==i:
temp.append(1)
else:
temp.append(ans[i-1][k-1]+ans[i-1][k])
ans.append(temp)
return ans
119题目解答相似,只是要输出temp即可
class Solution(object):
def getRow(self, rowIndex):
ans=[]
for i in range(rowIndex+1):
temp=[]
for k in range(i+1):
if k==0 or k==i:
temp.append(1)
else:
temp.append(ans[i-1][k-1]+ans[i-1][k])
ans.append(temp)
return temp