Pascal's Triangle I & II

LeetCode 118 and 119

class Solution:
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        """
        [1]
        [0,1] [1,0] -> zip -> (0,1) (1,0) -> [1, 1]
        [0, 1, 1] [1, 1, 0] -> zip -> (0, 1) (1, 1) (1, 0) -> [1, 2, 1]
        """
        row = [1]
        for _ in range(rowIndex):
            row = [x + y for x, y in zip([0]+row, row+[0])]
        return row
class Solution:
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        res = []
        
        for i in range(numRows):
            res.append([1]*(i+1))
            if i > 1:
                for j in range (1, i):
                    res[i][j] = res[i-1][j-1] + res[i-1][j]
        return res

Last updated