파비의 매일매일 공부기록

2023.09.08 Today's Challenge 본문

Problem Solving/LeetCode

2023.09.08 Today's Challenge

fabichoi 2023. 9. 8. 23:45

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

 

Pascal's Triangle - LeetCode

Can you solve this real interview question? Pascal's Triangle - 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: [https://upload.wikimedia.o

leetcode.com

오랫만에 스스로 풀어본 문제!

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        ar = [[], [1], [1, 1]]
        for i in range(3, 31):
            a = []
            for j in range(i):
                if j == 0 or j == i - 1:
                    a.append(1)
                    continue
                a.append(ar[i - 1][j - 1] + ar[i - 1][j])
            ar.append(a)
        return ar[1:numRows+1]
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.09.10 Today's Challenge  (0) 2023.09.10
2023.09.09 Today's Challenge  (0) 2023.09.09
2023.09.07 Today's Challenge  (0) 2023.09.07
2023.09.06 Today's Challenge  (0) 2023.09.06
2023.09.05 Today's Challenge  (0) 2023.09.05
Comments