엔지니어 게시판
LeetCode 솔루션 분류

[7/19] 118. Pascal's Triangle

컨텐츠 정보

본문

118. Pascal's Triangle
Easy
6587226Add to ListShare

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
Accepted
877,831
Submissions
1,321,677

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 31 ms, faster than 93.47% of Python3 online submissions for Pascal's Triangle.
Memory Usage: 13.9 MB, less than 66.48% of Python3 online submissions for Pascal's Triangle.
class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        ans = [[1]]
        
        for i in range(1, numRows):
            part = []
            
            if len(ans[-1]) == 1:
                ans.append([1,1])
            else:
                part.append(1)
                for j in range(len(ans[-1]) - 1):
                    part.append(ans[-1][j] + ans[-1][j+1])
                part.append(1)
                ans.append(part)
        return ans
전체 396 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 164 명
  • 오늘 방문자 2,479 명
  • 어제 방문자 5,793 명
  • 최대 방문자 11,134 명
  • 전체 회원수 1,106 명
알림 0