Gary Lai
  • Home
  • About
Sign in Subscribe

118. Pascal's Triangle

Last updated on  Oct 31, 2023

118. Pascal's Triangle

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        def helper(numRows):
            if numRows == 1:
                res.append([1])
                return [1]
            prev = helper(numRows - 1)
            curr = []
            for i in range(len(prev) - 1):
                curr.append(prev[i] + prev[i+1])
            res.append([1] + curr + [1])
            return [1] + curr + [1]

        res = []
        helper(numRows)
        return res
Previous 151. Reverse Words in a String
Next 119. Pascal's Triangle II
Gary Lai © 2025
  • Sign up
Powered by Ghost