119. Pascal's Triangle II

Posted whatyouthink

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了119. Pascal's Triangle II相关的知识,希望对你有一定的参考价值。

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.

只额外开O(k)的空间,那就开一个两行k列的数组,滚动记录上一次的结果就行

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        ans = [[1] * (rowIndex + 1), [1] * (rowIndex + 1)]
        for index in range(1, rowIndex + 2, 1):
            for i in range(1, index - 1, 1):
                ans[index % 2][i] = ans[(index - 1) % 2][i] + ans[(index - 1) % 2][i - 1]
        
        return ans[(rowIndex + 1) % 2]
            

 

以上是关于119. Pascal's Triangle II的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode_119. Pascal's Triangle II

119. Pascal's Triangle II

119. Pascal's Triangle II@python

119. Pascal's Triangle II

119. Pascal's Triangle II

119. Pascal's Triangle II