方法一:
def com( n, m):
if m==0:
return 1
result=1
result2=1
for i in range(1,m+1):
result=result*(n+1-i)/i
return int(result)
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
List=[[1]]
for i in range(1,numRows):
List.append([com(i,j) for j in range(i+1)])
return List
result=result*(n+1-i)/i
如果直接写result*=
后边的分数会先计算,除不尽影响精度
思路
C
n
m
C_n^m
Cnm
方法二:
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
result = []
for i in range(numRows):
now = [1]*(i+1)
if i >= 2:
for n in range(1,i):
now[n] = pre[n-1]+pre[n]
result += [now]
pre = now
return result```