Leetcode - pascal's triangle

更新於 發佈於 閱讀時間約 5 分鐘

Description

link

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:

raw-image

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

Intuition

The first core concept: Handle exceptions when numRows equals one and two.

The second core concept: Use recursion to traverse each level of the Pascal's triangle, and use loops to generate the elements of each layer.


Approach

According to the picture in the question, all of the elements in each layer is the sum of the two elements from the previous layer, except the elements in the first and the second layer, also the first and last elements of each layer.


Complexity

  • Time complexity: O(numRows²)
  • Space complexity: O(numRows²)


Code

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        self.pascal = [[1]]
        self.total = 0
        self.total = numRows
        if numRows == 1:
            return self.pascal
        self.pascal.append([1,1])
        if numRows == 2:
            return self.pascal
        self.recursion(3)
        return self.pascal

    def recursion(self, current_floor: int):
        row = []
        for i in range(current_floor):
            if i==0 or i==current_floor-1:
                row.append(1)
                continue
            row.append(self.pascal[current_floor-2][i-1] + self.pascal[current_floor-2][i])
        self.pascal.append(row)
        if self.total == current_floor:
            return
        return self.recursion(current_floor+1)


聽說幫這篇按讚的這個月發票都會中喔😘

留言
avatar-img
留言分享你的想法!
avatar-img
周濡墨的沙龍
17會員
119內容數
有別於未付費的文章,專題區的文章偏向愛情短篇小說,較有戲劇性,篇幅也會較長;未付費文章會偏向極短篇小說,並且以類似散文的手法寫作
周濡墨的沙龍的其他內容
2025/07/30
information link:Two Sum - LeetCode Description:Given an array of integers nums and an integer target, return indices of the two numbers such......
2025/07/30
information link:Two Sum - LeetCode Description:Given an array of integers nums and an integer target, return indices of the two numbers such......
2025/05/06
這篇文章將藉由 2w1h(what、why、how)來介紹如何透過 github 達到更好的團隊合作。 WHAT github 是甚麼 社交媒體有 ig、facebook 等可以分享自己的生活,而 github 則是工程師分享自己 code 或專案的地方。不過,這並不是 github 受歡迎之
Thumbnail
2025/05/06
這篇文章將藉由 2w1h(what、why、how)來介紹如何透過 github 達到更好的團隊合作。 WHAT github 是甚麼 社交媒體有 ig、facebook 等可以分享自己的生活,而 github 則是工程師分享自己 code 或專案的地方。不過,這並不是 github 受歡迎之
Thumbnail
2025/04/23
核心意識 自有平台 工作地點 職缺和實習 培訓 你不需要很厲害 QA 面試詳細內容:20分鐘線上程式測驗。沒有複試,一次面試。會從履歷中問問題 有沒有短期實習,例如暑期實習:希望暑假+一個學期。開學後每周三天沒有硬性規定哪幾天 資安推薦能力或證照:英文能力,e
Thumbnail
2025/04/23
核心意識 自有平台 工作地點 職缺和實習 培訓 你不需要很厲害 QA 面試詳細內容:20分鐘線上程式測驗。沒有複試,一次面試。會從履歷中問問題 有沒有短期實習,例如暑期實習:希望暑假+一個學期。開學後每周三天沒有硬性規定哪幾天 資安推薦能力或證照:英文能力,e
Thumbnail
看更多
你可能也想看
Thumbnail
2025 vocus 推出最受矚目的活動之一——《開箱你的美好生活》,我們跟著創作者一起「開箱」各種故事、景點、餐廳、超值好物⋯⋯甚至那些讓人會心一笑的生活小廢物;這次活動不僅送出了許多獎勵,也反映了「內容有價」——創作不只是分享、紀錄,也能用各種不同形式變現、帶來實際收入。
Thumbnail
2025 vocus 推出最受矚目的活動之一——《開箱你的美好生活》,我們跟著創作者一起「開箱」各種故事、景點、餐廳、超值好物⋯⋯甚至那些讓人會心一笑的生活小廢物;這次活動不僅送出了許多獎勵,也反映了「內容有價」——創作不只是分享、紀錄,也能用各種不同形式變現、帶來實際收入。
Thumbnail
題目敘述 題目會給我們一個鏈結串列的頭部結點Head node,要求我們計算鏈結串列中的Twin sum最大值是多少? 註: Twin Sum的定義就是頭尾結點相對位置相同的,互相配對加總在一起的值。 例如 給定串列= 1 -> 3 -> 2 -> 5 -> 100 -> 8 1, 8 一組
Thumbnail
題目敘述 題目會給我們一個鏈結串列的頭部結點Head node,要求我們計算鏈結串列中的Twin sum最大值是多少? 註: Twin Sum的定義就是頭尾結點相對位置相同的,互相配對加總在一起的值。 例如 給定串列= 1 -> 3 -> 2 -> 5 -> 100 -> 8 1, 8 一組
Thumbnail
題目敘述 題目會給我們一個輸入陣列nums,要求我們判斷輸入陣列nums內部是否存在長度為三的遞增子序列? 題目的原文敘述 測試範例 Example 1: Input: nums = [1,2,3,4,5] Output: true Explanation: Any triplet wh
Thumbnail
題目敘述 題目會給我們一個輸入陣列nums,要求我們判斷輸入陣列nums內部是否存在長度為三的遞增子序列? 題目的原文敘述 測試範例 Example 1: Input: nums = [1,2,3,4,5] Output: true Explanation: Any triplet wh
Thumbnail
題目敘述 題目會給兩個陣列nums1和nums2。 題目要求我們從中同步選擇長度為k的子序列,並且最大化子序列的分數, 回傳最高的分數值。 分數的定義: 分數 = (nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] ,
Thumbnail
題目敘述 題目會給兩個陣列nums1和nums2。 題目要求我們從中同步選擇長度為k的子序列,並且最大化子序列的分數, 回傳最高的分數值。 分數的定義: 分數 = (nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] ,
Thumbnail
題目敘述 題目會給我們泰伯納西數列的一般項和初始條件,要求我們實現找出第n項的function。 def tribonacci(self, n: int): 泰伯納西數列的一般項: Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0. 泰伯納西數列的初始條件: T0 = 0,
Thumbnail
題目敘述 題目會給我們泰伯納西數列的一般項和初始條件,要求我們實現找出第n項的function。 def tribonacci(self, n: int): 泰伯納西數列的一般項: Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0. 泰伯納西數列的初始條件: T0 = 0,
Thumbnail
題目敘述 題目會給我們一個輸入陣列nums,每個元素值代表那個格子點可以左右位移的固定長度。 例如,假設 nums[i] = 3,那麼下一步可以移動到nums[i-3] 或 nums[i+3]這兩個格子點。 題目​會給定一個起始點start索引位置,請問我們能不能走到內部數值為0的格子點?
Thumbnail
題目敘述 題目會給我們一個輸入陣列nums,每個元素值代表那個格子點可以左右位移的固定長度。 例如,假設 nums[i] = 3,那麼下一步可以移動到nums[i-3] 或 nums[i+3]這兩個格子點。 題目​會給定一個起始點start索引位置,請問我們能不能走到內部數值為0的格子點?
Thumbnail
題目敘述 題目的情境是設計並且實現一個包含所有正整數的數據流,以set集合的方式存在。 數據流 = {1, 2, 3, 4, ..., ∞} 要求我們去實現定義好的function介面: SmallestInfiniteSet()建構子,初始化這個包含所有正整數的數據流。 int po
Thumbnail
題目敘述 題目的情境是設計並且實現一個包含所有正整數的數據流,以set集合的方式存在。 數據流 = {1, 2, 3, 4, ..., ∞} 要求我們去實現定義好的function介面: SmallestInfiniteSet()建構子,初始化這個包含所有正整數的數據流。 int po
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News