119. Pascal's Triangle II

来源:互联网 发布:最好的变声软件 编辑:程序博客网 时间:2024/05/23 19:14

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):        List = [1]          if rowIndex <= 0 :              return List          List = [i for i in range(rowIndex+1)]          for i in range(rowIndex+1):              List[i] = []          if rowIndex == 1:              List[0].append(1)              List[1].extend([1,1])          else:              List[0].append(1)              List[1].extend([1,1])              for i in range(2,rowIndex+1):                  cnt = len(List[i-1])                  List[i].append(1)                  for j in range(cnt-1):                      List[i].append(List[i-1][j]+List[i-1][j+1])                  List[i].append(1)          return List[rowIndex]  


原创粉丝点击