118. Pascal's Triangle

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.

pascal 三角形

a[i][j] = a[i - 1][j - 1] + a[i - 1][j] 

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        ans = []
        for i in range(0, numRows, 1):
            sub = []
            for j in range(0, i + 1, 1):
                if j == 0 or j == i:
                    sub.append(1)
                else:
                    sub.append(ans[i - 1][j] + ans[i - 1][j - 1])
            ans.append(sub)
        return ans

 

你可能感兴趣的:(118. Pascal's Triangle)