Leetcode119-Pascal's Triangle II

2017-09-20  本文已影响0人  LdpcII

119. Pascal's Triangle II

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?

My Solution

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        result = [1]
        while(len(result) < rowIndex+1):
            result = list(map(lambda x, y: x + y, [0] + result, result + [0]))
        return result

Reference (转)

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        row = [1]
        for _ in range(rowIndex):
            row = [x + y for x, y in zip([0]+row, row+[0])]
        return row
上一篇下一篇

猜你喜欢

热点阅读