얼렁뚱땅 스며드는 Data Science

Algorithm/Daily Coding Tests Challenge

[Leetcode] Easy: Pascal's Triangle 2

Jesip14 2021. 10. 10. 20:02

Description

Given an integer rowIndex, return the rowIndexth (0-indexed) row of the 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 getRow(self, rowIndex: int) -> List[int]:
        array = [1] * 34
        array[0] = [1]
        array[1] = [1,1]
        
        for i in range(2,34):
            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[rowIndex]

 

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

 

Pascal's Triangle II - 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