【leetcode】Pascal's Triangle II

题目简述:

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?

解题思路:

这里的关键是空间的使用,既然只能用O(K)很容易就想到我们要进行回卷(名字好像不对)。我的做法是每一次都在后面新加入一个数

class Solution:
    # @return a list of integers
    def getRow(self, rowIndex):
        ans = [1] * (rowIndex+1)
        for i in range(rowIndex):
            for j in range(i,0,-1):
                ans[j] += ans[j-1]
            #print ans
        return ans
posted @ 2015-03-29 20:11  mrbean  阅读(212)  评论(0编辑  收藏  举报