问题描述:
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
解决方案:
class Solution(object):
def getRow(self, rowIndex):
""" :type rowIndex: int :rtype: List[int] """
result = [1]
for num in range(rowIndex):
result = [sum(i) for i in zip([0] + result, result + [0])]
return result
思路说明:
下一行的结果 = 上一行复制两份,错位相加(空位补0)。如:
[1,1] = [0,1] + [1,0]
[1,2,1] = [0,1,1] + [1,1,0]
[1,3,3,1] = [0,1,2,1] + [1,2,1,0]
...