leetcode @python 119. Pascal's Triangle II

题目链接

https://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].

题目大意

给定一个整数k,返回帕斯卡三角形的第k行(序号从0开始)

解题思路

和上一题类似

代码

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        ans = [0] * (rowIndex + 1)
        for i in range(rowIndex + 1):
            if i == 0:
                ans[i] = [1]
            elif i == 1:
                ans[i] = [1, 1]
            else:
                ans[i] = [0] * (i + 1)
                ans[i][0] = ans[i][i] = 1
                for j in range(1, i):
                    ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j]
        return ans[rowIndex]
posted @ 2016-03-25 11:32  slurm  阅读(118)  评论(0编辑  收藏  举报