118. Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

 

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        r_list=[]
        if numRows<1:
            return r_list
        last_list=[]    
        for i in range(1,numRows+1):
            inner_list=[1]*i 
            for index in range(len(last_list)):
                if 0<index<i-1:
                    inner_list[index]=last_list[index-1]+last_list[index]
            
            r_list.append(inner_list)
            last_list=inner_list
            
        return r_list

 

posted @ 2017-01-12 16:51  rocksolid  阅读(125)  评论(0编辑  收藏  举报