Python AC code for reference


  • 1
    B
    class Solution:
    # @return a list of lists of integers
    # do it dynamical programming way.
    def generate(self, numRows):
        i=0
        res=[]
        while i<numRows:
            if i==0:
                temp=[1]
                res.append(temp)
            elif i==1:
                temp=[1,1]
                res.append(temp)
            else:
                prev=res[i-1]
                temp=[1]
                for j in range(1,i):
                    temp.append(prev[j-1]+prev[j])
                temp.append(1)
                res.append(temp)
            i+=1
                
        return res
    

    here is a rather self-explanatory implementation of the triangle.


  • 0
    S
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.