美文网首页
Leetcode119-Pascal's Triangle II

Leetcode119-Pascal's Triangle II

作者: LdpcII | 来源:发表于2017-09-20 16:37 被阅读0次

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

相关文章

网友评论

      本文标题:Leetcode119-Pascal's Triangle II

      本文链接:https://www.haomeiwen.com/subject/mrposxtx.html