119. Pascal triangle II

Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal’s triangle. In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown: Example 1: Input: rowIndex = 3 Output: [1,3,3,1] Example 2: Input: rowIndex = 0 Output: [1]

  • code
class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        if rowIndex == 0:
            return [1]
        elif rowIndex == 1:
            return [1,1]
        
        def next(row):
            cur = [1]
            for i in range(len(row)-1):
                cur.append(row[i]+row[i+1])
            cur.append(1)
            return cur

        return next(self.getRow(rowIndex-1))



  • code
class Solution:
    def getRow(self, rowIndex):
        pascal = [1]*(rowIndex + 1)
        for i in range(2,rowIndex+1):
            for j in range(i-1,0,-1):
                pascal[j] += pascal[j-1]
        return pascal