您的位置:首页 > 其它

119. Pascal's Triangle II*

2016-08-31 16:27 274 查看
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?

Reference

class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
result = [0]*(rowIndex+1)
result[0]= 1
for i in range(1,rowIndex+1):
for j in range(i,0,-1):
result[j] +=result[j-1]
return result

Note:
It is obvious that we should find the law of numbers in certain row of Pascal's Triangle.

According to materials online, the nth number in mth row is C(m,n-1).However, this method needs a lot of computation.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: