Given an index k, return the kth row of the Pascal's triangle.

For example, givenk= 3,
Return[1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

URL: https://leetcode.com/problems/pascals-triangle-ii/

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        if rowIndex < 0:
            return []
        elif rowIndex == 0:
            return [1]
        else:
            pre = []
            pre.append(1)
            for i in range(1, rowIndex+1):
                curr = []
                curr.append(1)
                for j in range(0, len(pre) - 1):
                    curr.append(pre[j]+pre[j+1])
                curr.append(1)
                pre = curr

            return pre

results matching ""

    No results matching ""