aboutsummaryrefslogtreecommitdiff
path: root/pascals_triangle.py
blob: 0b2bf08a9a2bc945d8654fb7f49f8ab4863a11e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 118. Pascal's Triangle

"""
given an integer 'num_rows', return the first numrows of pascal's triangle.
in pascal's triangle, each number is the sum of the two number direction
above it as show [gif]
"""


class Solution(object):
    def generate(self, num_rows):
        ans = [[1 for _ in range(row + 1)] for row in range(num_rows)]
        for row in range(num_rows):
            for col in range(1, row):
                ans[row][col] = ans[row - 1][col] + ans[row - 1][col - 1]
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.generate(5))  # expect: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
    print(obj.generate(1))  # expect: [[1]]