【leetcode】Pascal's Triangle II

題目簡述:

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

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

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

解題思路:

這裏的關鍵是空間的使用,既然只能用O(K)很容易就想到咱們要進行回捲(名字好像不對)。個人作法是每一次都在後面新加入一個數it

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
相關文章
相關標籤/搜索