118. Pascal's Triangle@python

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

Example:

Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

原题地址: Pascal's Triangle

难度: Easy

题意: 杨辉三角

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

时间复杂度: O(n)

空间复杂度: O(n)

posted @ 2018-09-30 18:27  静静地挖坑  阅读(103)  评论(0)    收藏  举报