6/24/2020

[LeetCode] 118. Pascal's Triangle

Problem : https://leetcode.com/problems/pascals-triangle/

Calculate current line base on the last line numbers.

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        result = []
        
        if numRows <= 0:
            return result
    
        for i in range(numRows):
            result.append([1] * (i+1))
            
        for i in range(2, numRows):    
            for j in range(1, i):
                result[i][j] = result[i-1][j-1] + result[i-1][j]
            
        return result

No comments:

Post a Comment