Zero to Hero
article thumbnail
Published 2021. 6. 7. 21:37
118. Pascal's Triangle Algorithm
 

Pascal's Triangle - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

1. 구현

from typing import *


class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        answer = [[1]]
        if numRows == 1:
            return answer
        elif numRows == 2:
            answer.append([1, 1])
            return answer
        else:
            answer.append([1, 1])
            for index in range(2, numRows):
                cur, temp = 0, []
                while cur < index + 1:
                    if cur in [0, index]:
                        temp.append(1)
                    else:
                        temp.append(answer[index - 1][cur - 1] + answer[index - 1][cur])
                    cur += 1
                answer.append(temp)
            return answer

 

 

파스칼의 삼각형 - 위키백과, 우리 모두의 백과사전

위키백과, 우리 모두의 백과사전. 파스칼의 삼각형(Pascal's triangle)은 수학에서 이항계수를 삼각형 모양의 기하학적 형태로 배열한 것이다. 이것은 블레즈 파스칼에 의해 이름 붙여졌으나 이미 수

ko.wikipedia.org

파스칼의 삼각형을 구하는 문제다.

위 풀이가 DP라고 말할 수 있는가는 약간 의문이지만;

'Algorithm' 카테고리의 다른 글

172. Factorial Trailing Zeroes  (0) 2021.06.10
125. Valid Palindrome  (0) 2021.06.08
26. Remove Duplicates from Sorted Array  (0) 2021.06.06
13. Roman to Integer  (0) 2021.05.31
202. Happy Number  (0) 2021.05.30
profile

Zero to Hero

@Doljae

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!