码迷,mamicode.com
首页 > 其他好文 > 详细

119. Pascal's Triangle II

时间:2020-07-07 15:03:33      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:for   return   数组   nbsp   div   not   pre   The   结果   

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

标签:for   return   数组   nbsp   div   not   pre   The   结果   

原文地址:https://www.cnblogs.com/whatyouthink/p/13260505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!