LeetCode 119 Pascal's Triangle II (Python)

Posted by 小明MaxMing on August 12, 2020

题目

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal’s triangle.

Note that the row index starts from 0.

解题思路

对于每一行,从后向前求,就不需要额外空间了

代码

class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        res = [1]
        for i in range(rowIndex):
            for j in range(i, 0, -1):
                res[j] += res[j - 1]
            res.append(1)
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅