首页 > 其他 > 详细

118. Pascal's Triangle

时间:2020-07-06 11:09:35      阅读:48      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/whatyouthink/p/13253675.html

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