Total Accepted: 105799
Total Submissions: 297504
Difficulty: Easy
Contributors: Admin
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(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
tmp = [1]
i = 0
while(i != rowIndex):
tmp = map(lambda x, y: x+y, tmp+[0], [0]+tmp)
i += 1
return tmp

本文介绍了一个算法问题:如何求出帕斯卡三角形的第K行。使用了迭代的方法,仅需O(K)的空间复杂度,提供了一种高效解决此问题的Python实现。
7883

被折叠的 条评论
为什么被折叠?



