Simple PYTHON - iterative, concise/fast


  • 0
    V
    class Solution(object):
    class Solution(object):
        def generate(self, numRows):
            """
            :type numRows: int
            :rtype: List[List[int]]
            """
            ret = []
            if numRows <= 0:
                return ret
            
            rowsBuilt = 0
            while rowsBuilt < numRows:
                pos = 0
                currRow = []
                while pos < rowsBuilt + 1:
                    if pos == 0 or pos == rowsBuilt:
                        currRow.append(1)
                    else:
                        currRow.append(ret[rowsBuilt - 1][pos] + ret[rowsBuilt - 1][pos - 1])
                    pos += 1
                ret.append(currRow)
                rowsBuilt += 1
    
            return ret
    

Log in to reply
 

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