[leetcode]Pascal's Triangle II @ Python

原题地址:https://oj.leetcode.com/problems/pascals-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?

解题思路:思路同上一道题。

class Solution:
    # @return a list of integers
    def getRow(self, rowIndex):
        if rowIndex == 0: return [1]
        if rowIndex == 1: return [1, 1]
        list = [[] for i in range(rowIndex+1)]
        list[0] = [1]
        list[1] = [1, 1]
        for i in range(2, rowIndex+1):
            list[i] = [1 for j in range(i + 1)]
            for j in range(1, i):
                list[i][j] = list[i - 1][j - 1] + list[i - 1][j]
        return list[rowIndex]
原文地址:https://www.cnblogs.com/zuoyuan/p/3766392.html