leetcode-easy-others-118 Pascal's Triangle

mycode   16.47%

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        if not numRows : return []
        elif numRows == 1 : return [[1]]
        res = [[1],[1,1]]
        for i in range(2,numRows):
            temp = [1]
            for j in range(1,i):
                temp.append(res[i-1][j-1] + res[i-1][j])
            temp.append(1)
            res.append(temp)
        return res

 

reference

class Solution (Object):
     DEF Generate (Self, numRows):
         "" " 
        : type numRows: int 
        : rtype: List [List [int]] 
        " "" 
        Triangle = [[. 1] * n- for n- in Range (. 1, +. 1 numRows )] 
        
        for I in Range (2, numRows):   # third row starts 
            for J in Range (. 1, I):   # a second column to the last column of the second 
                triangle [i] [j] = triangle [i -1] [-J. 1] + Triangle [. 1-I ] [J]           
         return Triangle

 

Guess you like

Origin www.cnblogs.com/rosyYY/p/11006191.html