Simple Iterative Python Solution

  • 0
        def generate(self, numRows):
            :type numRows: int
            :rtype: List[List[int]]
            triangle = [[1]]
            for _ in xrange(numRows-1):
                curr_level = [1]
                prev_level = triangle[-1]
                for i in xrange(len(prev_level)-1):
                    curr_level.append(prev_level[i] + prev_level[i+1])
                triangle += [curr_level]
            return triangle[1:] if not numRows else triangle

Log in to reply

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