thiagowfx's avatar

¬ just serendipity 🍀 (not just serendipity)

LeetCode #118: Pascal's Triangle

• 52 words • 1 min • updated

LeetCode #118: Pascal’s Triangle:

python
class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        ans = []

        for r in range(1, numRows + 1):
            row = [1] * r

            if r > 2:
                for j in range(1, r - 1):
                    prev_row = ans[-1]
                    row[j] = prev_row[j - 1] + prev_row[j]

            ans.append(row)

        return ans