Simple iterative Python, 4 lines, 40 ms

  • 2

    Each new line is calculate from previous line

    def generate(self, numRows):
        res = [[1]]
        for i in range(1, numRows):
            res += [[1] + [res[i-1][j]+res[i-1][j+1] for j in range(i-1)] + [1]]
        return res if numRows else []

Log in to reply

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