您的位置:首页 > 编程语言 > Python开发

leetcode @python 119. Pascal's Triangle II

2016-03-25 11:32 525 查看

题目链接

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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: