Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
Subscribe to see which companies asked this question
利用杨辉三角的公式,算
第n行的m个数可表示为C(n-1,m-1)
class Solution(object):
def cal(self,a,b):
res = 1
for i in range(0,b):
res *= (a-i)
for i in range(1,b+1):
res /= i
return res
def getRow(self, rowIndex):
r = rowIndex
res = [0]*(r+1)
#temp = [0]*(r+1)
for i in range(0,r+1):
res[i] = self.cal(r,i)
#print res
return res
"""
:type rowIndex: int
:rtype: List[int]
"""