물음표 살인마의 개발블로그

알고리즘 문제/Leetcode

118. Pascal's Triangle

BEstyle 2022. 12. 8. 01:35

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:

 

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:
    def generate(self, numRows: int) -> List[List[int]]:
        res=[[1]]
        for i in range(numRows-1):
            temp=[0] + res[-1] +[0]
            tlist=[]
            for j in range(len(res[-1])+1):
                tlist.append(temp[j]+temp[j+1])
            res.append(tlist)
        return res

'알고리즘 문제 > Leetcode' 카테고리의 다른 글

121. Best Time to Buy and Sell Stock  (0) 2022.12.08
119. Pascal's Triangle II  (0) 2022.12.08
70. Climbing Stairs  (0) 2022.12.07
11. Container With Most Water  (0) 2022.12.07
328. Odd Even Linked List  (0) 2022.12.06