얼렁뚱땅 스며드는 Data Science

Algorithm/Daily Coding Tests Challenge

[Leetcode] Easy : Pascal's Triangle

Jesip14 2021. 10. 10. 19:58

Description

Given an integer numRows, return the first numRows of Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

My solution

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

 

문제 출처 : https://leetcode.com/problems/pascals-triangle/

 

Pascal's Triangle - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com