关闭

119. Pascal's Triangle II*

54人阅读 评论(0) 收藏 举报
分类:

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

Reference

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

Note:

It is obvious that we should find the law of numbers in certain row of Pascal's Triangle.

According to materials online, the nth number in mth row is C(m,n-1).However, this method needs a lot of computation.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:61109次
    • 积分:3903
    • 等级:
    • 排名:第8480名
    • 原创:332篇
    • 转载:90篇
    • 译文:0篇
    • 评论:11条
    最新评论