LeetCode 118.杨辉三角(简单)

不羁的心 提交于 2019-11-28 07:16:34

题目描述:

思路:

  1. 当前行头元素先追加 1
  2. 当前行除了第一和最后一个元素的值(i,j)=上一行(i-1,j-1)+上一行(i-1,j)
  3. 遍历完,当前行追加1
class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        res=[]
        while numRows:
            temp=[1]
            if not res:
                res.append(temp)
            else:
                n=len(res[-1])
                for i in range(n-1):
                    temp.append(res[-1][i]+res[-1][i+1])
                    # temp.append(su)
                temp.append(1)
                res.append(temp)
            numRows-=1
        return res

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!