Day10-119. Pascal's Triangle II

今日题目:119. Pascal's Triangle II

Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
https://ithelp.ithome.com.tw/upload/images/20210925/201408438Cwo2Ybl9l.png

思路

之前就写过PasCal's Triangle了
从row开始依序向下计算,
最左、最右直接append(1)
中段就由上一row的加起来

My solution

class Solution:
    def getRow(self, numRows: int) -> List[List[int]]:
        triangle = [[1]]
        ans = []
        for i in range(1,numRows+1):
            for j in range(i+1):
                if j == 0 or j==i:
                    ans.append(1)
                else:
                    ans.append(triangle[i-1][j-1] + triangle[i-1][j])

            triangle.append(ans)
            ans = []

        return triangle[numRows]

Result

https://ithelp.ithome.com.tw/upload/images/20210925/20140843DzgJxz960t.png


<<:  Day10:终於要进去新手村了-Javascript-变数

>>:  [Day 14] 第一主餐 pt.7-一支API,千万request来相见

如何申请免费 Let’s Encrypt SSL 自动更新凭证,自架 IIS 站台适用

Https 连线网页使用 SSL 加密凭证可以让使用者在网页输入的资料更加安全,减少被截取内容的风险...

【网页设计 入门 】如何使用 Bootstrap 与 Github Pages 制作 个人网站 ?

简单架设 x 不失质感 目录 源起 : 开发者网站 开发工具 : Adobe Brackets 基础...

day 21 - NSQ Producer

Producer是讯息发送方, 他会对nsqd发送讯息, nsqd支援TCP(port:4150) ...

就决定是你了 - 阵列系列I

图片来源:tooto1985/js-array-operations 内心剧场之胡言乱语 万能又好...

【Day06】生命周期 Lifecycle(Class Component)

React 元件拥有从产生、渲染 到被移除解放资源的各个阶段 称之为生命周期(Lifecycle)。...