Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 1.23 KB

풀이_LC118.md

File metadata and controls

42 lines (30 loc) · 1.23 KB

💰 LeetCode 118. Pascal's Triangle

  • Date : 2021.06.20(일)
  • Time : 10분

Problem

  • 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:

Constraints

  • 1 <= numRows <= 30

Example

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



풀이

    def generate(self, numRows: int) -> List[List[int]]:
        if numRows == 1:
            return [[1]]
        
        answer = []

        for i in range(1,numRows+1):
            answer.append([0]*i)
        for i in range(0,numRows):
            answer[i][0] = 1
            answer[i][-1] = 1

        for i in range(1,numRows):
            for j in range(0, len(answer[i]),1):
                if answer[i][j] == 0 :
                    answer[i][j] = answer[i-1][j-1] + answer[i-1][j]


        return answer

: 먼저 파스칼의 삼각형 모양의 배열을 생성해주고 한 행마다 맨 앞과 맨 뒤의 값만 1로 모두 바꿔준 뒤 시작하였다. 파스칼 삼각형의 값을 구하려면 [i-1][j-1] + [i-1][j] 값이기 때문에 값이 0일때만 이 식으로 값을 구해서 다시 넣어주었다.