2 simple python solutions


  • 1
    A

    solution 1 uses python map() function.

    solution 2 uses python List Comprehensions.

    class Solution:
        def generate(self, numRows):
            answer = list()
            if numRows == 0: return answer
            answer.append([1])
            if numRows == 1: return answer
            
            from operator import add  # for solution 1
            for i in range(1, numRows):
                new = map(add, (answer[i-1] + [0]), ([0] + answer[i-1]))  # solution 1
                # new = [sum(x) for x in zip((answer[i-1] + [0]), ([0] + answer[i-1]))]  # solution 2
                
                answer.append(new)
        
            return answer

Log in to reply
 

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