My easily understanding python solution


  • 0
    F
    class Solution:
    # @param rowIndex, an integer
    # @return an integer[]
    def getRow(self, rowIndex):
        prevRow=[]
        if rowIndex<0:return prevRow
        if rowIndex==0:
            prevRow=[1]
            return prevRow
        if rowIndex==1:
            prevRow=[1,1]
            return prevRow
        prevRow=[1,1]
        curRow=[]
        for i in xrange(3,rowIndex+2):
            for j in xrange(1,i+1):
                if j==1 or j==i:
                    curRow.append(1)
                else:
                    curRow.append(prevRow[j-2]+prevRow[j-1])
            prevRow=curRow
            curRow=[]
        return prevRow

  • 1
    L

    Mine

    class Solution:
        # @param {integer} rowIndex
        # @return {integer[]}
    
        def getRow(self, rowIndex):
            if rowIndex <= 0:
                return [1]
    
            prev = self.getRow(rowIndex - 1)
            result = []
            
            # first element
            result.append(prev[0])
            
            # middle elements
            for i in range(1, rowIndex):
                result.append(prev[i - 1] + prev[i])
                
            # last elements
            result.append(prev[rowIndex - 1])
    
            return result
    

Log in to reply
 

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