您的位置:首页 > 编程语言 > Python开发

<LeetCode><Easy> 118 Pascal's Triangle II

2015-10-16 18:24 393 查看
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?

#Python2 TimeOut

#递归

偏移求和【0】+L(k) 与L(k)+【0】--> L(k+1)

class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
get=lambda k:map(lambda x,y:x+y,get(k-1)+[0],[0]+get(k-1)) if k>1 else [1,1] if k==1 else [1]
get(rowIndex)

#是组合数的列写
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
fatorial=lambda x:fatorial(x-1)*x if x>1 else 1
combination=lambda m,n:fatorial(m)/fatorial(n)/fatorial(m-n)
if rowIndex%2:
fore=[combination(rowIndex,i) for i in range(rowIndex/2+1)]
return fore+fore[::-1]
else:
fore=[combination(rowIndex,i) for i in range(rowIndex/2+1)]
return fore+fore[::-1][1:]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode python