[leetcode] 118. Pascal's Triangle

섬섬's 개발일지·2022년 1월 29일
0

leetcode

목록 보기
21/23

118. Pascal's Triangle

Problem

Given an intenger 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:

Example 1:

Input ; numRows = 5
Output : [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Example 2:

Input : numRows = 1
Output : [[1]]

Constraints:

  • 1<= numRows <= 30

코드

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        if numRows == 1 :
            return [[1]]
        elif numRows == 2 :
            return [[1],[1,1]]
        
        result = [[1],[1,1],[1]]
        for i in range(2,numRows) :
            for j in range(1,i) :
                result[i].append(result[i-1][j-1] + result[i-1][j])
            result[i].append(1)
            result.append([1])
        
        return result[:-1]
profile
섬나라 개발자

0개의 댓글