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

来源:互联网 发布:网络零售交易额指什么 编辑:程序博客网 时间:2024/05/18 02:22

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:]


0 0
原创粉丝点击