每天(?)一道Leetcode(12) Pascal's Tri
2019-01-26 本文已影响0人
失业生1981
Array
119. Pascal's Triangle II
Given a non-negative index where , return the index row of the Pascal's triangle.
Note that the row index starts from .
即输出杨辉三角中的第k行
杨辉三角的每一行组合数其实可以通过计算得到
比如第三行是 对应
所以只要知道计算方法就ok,
[yeah~~~这是我自己做的第二个beats ]
class Solution:
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
res = []
k = rowIndex
nums = jc(k)
for i in range(k+1):
res.append(int(nums/(jc(i)*jc(k-i))))
return res
def jc(n):
if n==0:
return 1
else:
s = 1
for i in range(1,n+1):
s = s*i
return s