↓快轉到主要內容
  1. LeetCode/

LeetCode 733: Flood Fill

·1 分鐘· ·
LeetCode Easy Array Depth-First-Search Breadth-First-Search Matrix
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: easy 第一次嘗試:2022-09-03

  • 提交紀錄:Runtime Error ×6 → Accepted → Runtime Error ×12 → Accepted → Runtime Error ×5 → Accepted ×2

解題思路
#

連通塊(BFS 模板二):記下起點原本的顏色 old,從起點往上下左右擴散,只有在範圍內而且顏色是 old 的格子才換色並繼續。old == color 時要直接回傳,不然換完的格子還是 old,會一直重複走下去。

我的嘗試
#

這題在同一個晚上提交了 27 次,幾乎全部是 Runtime Error:

  • 第一版用 queue 但檢查的是對角線的四個方向。
  • 改成遞迴 DFS 之後,邊界寫成 x > row and y > column:用了 and(應該是 or),而且 row、column 對調了,一直 IndexError。
  • 中間幾版沒有處理「新顏色跟舊顏色一樣」,換完的格子還是會被當成要換的,無限遞迴。
  • 最後把邊界改成四個 or,並加上 image[r][c] == color 就停,才 AC。

正確解法
#

class Solution:
    def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
        old = image[sr][sc]
        if old == color:
            return image
        m, n = len(image), len(image[0])
        stack = [(sr, sc)]
        image[sr][sc] = color
        while stack:
            i, j = stack.pop()
            for x, y in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
                if 0 <= x < m and 0 <= y < n and image[x][y] == old:
                    image[x][y] = color
                    stack.append((x, y))
        return image

複雜度
#

時間 O(mn),空間 O(mn)。

要特別避免的錯誤
#

  • 邊界檢查是 x < 0 or y < 0 or x >= m or y >= n,四個條件用 or,不是 and。
  • 列數是 len(image),欄數是 len(image[0]),不要對調。
  • 新舊顏色相同時直接回傳。
  • 只有上下左右四個方向,不是對角線。

面試口說整理
#

我記下起點原本的顏色,從起點往上下左右擴散,在範圍內而且顏色相同的格子就換色並繼續;新舊顏色一樣時直接回傳,避免無限迴圈。時間 O(mn)。

相關文章

LeetCode 463: Island Perimeter
·1 分鐘
LeetCode Easy Array Depth-First-Search Breadth-First-Search Matrix
LeetCode 463 解題筆記:每格陸地貢獻 4 條邊,每跟上方或左方的陸地相鄰一次就少 2 條
LeetCode 637: Average of Levels in Binary Tree
·1 分鐘
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 637 解題筆記:層序遍歷,每層加總除以節點數
LeetCode 661: Image Smoother
·1 分鐘
LeetCode Easy Array Matrix
LeetCode 661 解題筆記:每格取周圍 3 × 3 範圍內存在的格子平均,向下取整;結果要寫到新的矩陣
LeetCode 559: Maximum Depth of N-ary Tree
·1 分鐘
LeetCode Easy Tree Depth-First-Search Breadth-First-Search
LeetCode 559 解題筆記:深度等於 1 加上所有小孩深度的最大值;沒有小孩時是 1
LeetCode 566: Reshape the Matrix
·1 分鐘
LeetCode Easy Array Matrix Simulation
LeetCode 566 解題筆記:元素總數不同就回傳原矩陣;否則第 k 個元素放到 (k // c, k % c)
LeetCode 404: Sum of Left Leaves
·1 分鐘
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 404 解題筆記:DFS 時告訴小孩自己是不是左小孩,只有「是左小孩而且是葉子」才加