首页 > 编程语言 > 详细

118. Pascal's Triangle@python

时间:2018-09-30 19:03:03      阅读:203      评论:0      收藏:0      [点我收藏+]

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)

118. Pascal's Triangle@python

原文:https://www.cnblogs.com/chimpan/p/9733018.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!